I am working with the pandas library and I want to add two new columns to a dataframe df with n columns (n > 0). These new columns result from the application of a function to one of the columns in the dataframe. The function to apply is like: One method for creating a new column for a function returning
Tag: python
Python lxml – get index of tag’s text
I have an xml-file with a format similar to docx, i.e.: I need to get an index of BIG_TEXT in source xml, like: I can start a new search from position of current index + len(text), but is there another way? Element may have one character, w for example. It will find index of w, but not index of tag
Removing _id element from Pymongo results
I’m attempting to create a web service using MongoDB and Flask (using the pymongo driver). A query to the database returns documents with the “_id” field included, of course. I don’t want to send this to the client, so how do I remove it? Here’s a Flask route: This returns: I tho…
How to open ssl socket using certificate stored in string variables in python
In Python, ssl.wrap_socket can read certificates from files, ssl.wrap_socket require the certificate as a file path. How can I start an SSL connection using a certificate read from string variables? My host environment does not allow write to files, and tempfile module is not functional I’m using Python…
How to implement server push in Flask framework?
I am trying to build a small site with the server push functionality on Flask micro-web framework, but I did not know if there is a framework to work with directly. I used Juggernaut, but it seems to be not working with redis-py in current version, and Juggernaut has been deprecated recently. Does anyone has …
NumPy: function for simultaneous max() and min()
numpy.amax() will find the max value in an array, and numpy.amin() does the same for the min value. If I want to find both max and min, I have to call both functions, which requires passing over the (very big) array twice, which seems slow. Is there a function in the numpy API that finds both max and min with
How to exclude a file from coverage.py?
I use nosetest’s coverage.py plugin. Is it somehow possible to exclude entire files or folders from the coverage report? My use case is having an external library in my project folder that obviously isn’t covered by my test suite. Answer Yeah, they have pretty extensive support for this in the doc…
Regular expression in Python won’t match end of a string
I’m just learning Python, and I can’t seem to figure out regular expressions. I want this code to print ‘yes’, but it obstinately prints ‘no’. I’ve also tried each of the following: Plus countless other variations. I’ve been searching for quite a while, but can&…
Is it possible to have an optional with/as statement in python?
Instead of this: it’s better to use this: What if I have something like this? Where do_something also has an “if FILE is None” clause, and still does something useful in that case – I don’t want to just skip do_something if FILE is None. Is there a sensible way of converting this…
Why is lxml.etree.iterparse() eating up all my memory?
This eventually consumes all my available memory and then the process is killed. I’ve tried changing the tag from schedule to ‘smaller’ tags but that didn’t make a difference. What am I doing wrong / how can I process this large file with iterparse()? I can easily cut it up and process…