I have the following code in Python 3.1 Which gives a string of 9.038768E-08, but I want the string 9.038768E-8 with the leading 0 of E-08 removed. How should I go about this? Answer you could do something like this or better as JBernardo suggests You need to do a replace for E+0 as well if you have big numbe…
Tag: python
Altering an Enum field using Alembic
How can I add an element to an Enum field in an alembic migration when using a version of PostgreSQL older than 9.1 (which adds the ALTER TYPE for enums)? This SO question explains the direct process, but I’m not quite sure how best to translate that using alembic. This is what I have: The above unfortu…
Is there a standardized method to swap two variables in Python?
In Python, I’ve seen two variable values swapped using this syntax: Is this considered the standard way to swap two variable values or is there some other means by which two variables are by convention most usually swapped? Answer Python evaluates expressions from left to right. Notice that while evalua…
Don’t put html, head and body tags automatically, beautifulsoup
I’m using beautifulsoup with html5lib, it puts the html, head and body tags automatically: Is there any option that I can set, turn off this behavior ? Answer This parses the HTML with Python’s builtin HTML parser. Quoting the docs: Unlike html5lib, this parser makes no attempt to create a well-fo…
how do I use key word arguments with python multiprocessing pool apply_async
I’m trying to get to grips with pythons multiprocessing module, specifically the apply_async method of Pool. I’m trying to call a function with arguments and keyword arguments. If I call the function without kwargs it’s fine but when I try to add in a keyword argument I get: TypeError: apply…
Matplotlib discrete colorbar
I am trying to make a discrete colorbar for a scatterplot in matplotlib I have my x, y data and for each point an integer tag value which I want to be represented with a unique colour, e.g. typically tag will be an integer ranging from 0-20, but the exact range may change so far I have just used the
Adding a y-axis label to secondary y-axis in matplotlib
I can add a y label to the left y-axis using plt.ylabel, but how can I add it to the secondary y-axis? Answer The best way is to interact with the axes object directly
Missing files for `magic` library on Windows
I need to get mime type for some files on windows, so i’ve installed python-magic (on 32-bit python 2.7.3). It depends on unix magic library. Author instructs to get regex2.dll, zlib1.dll and magic1.dll from gnuwin32 project. So i saved the files to a folder and added the folder to my system PATH. Now w…
How to check whether optional function parameter is set
Is there an easy way in Python to check whether the value of an optional parameter comes from its default value, or because the user has set it explicitly at the function call? Answer Lot of answers have little pieces of the full info, so I’d like to bring it all together with my favourite pattern(s). d…
How to split a dataframe string column into two columns?
I have a data frame with one (string) column and I’d like to split it into two (string) columns, with one column header as ‘fips’ and the other ‘row’ My dataframe df looks like this: I do not know how to use df.row.str[:] to achieve my goal of splitting the row cell. I can use df…