how do you filter pandas dataframes by multiple columns

Using & operator, don’t forget to wrap the sub-statements with (): males = df[(df[Gender]==’Male’) & (df[Year]==2014)] To store your dataframes in a dict using a for loop: from collections import defaultdict dic={} for g in [‘male’, ‘female’]: dic[g]=defaultdict(dict) for y in [2013, 2014]: dic[g][y]=df[(df[Gender]==g) & (df[Year]==y)] #store the DataFrames to a dict of dict EDIT: … Read more

Python urllib2 Basic Auth Problem

The problem could be that the Python libraries, per HTTP-Standard, first send an unauthenticated request, and then only if it’s answered with a 401 retry, are the correct credentials sent. If the Foursquare servers don’t do “totally standard authentication” then the libraries won’t work. Try using headers to do authentication: import urllib2, base64 request = … Read more

ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings

I figured that the DJANGO_SETTINGS_MODULE had to be set some way, so I looked at the documentation (link updated) and found: export DJANGO_SETTINGS_MODULE=mysite.settings Though that is not enough if you are running a server on heroku, you need to specify it there, too. Like this: heroku config:set DJANGO_SETTINGS_MODULE=mysite.settings –account <your account name> In my specific … Read more

Is it possible to get color gradients under curve in matplotlib?

There have been a handful of previous answers to similar questions (e.g. https://stackoverflow.com/a/22081678/325565), but they recommend a sub-optimal approach. Most of the previous answers recommend plotting a white polygon over a pcolormesh fill. This is less than ideal for two reasons: The background of the axes can’t be transparent, as there’s a filled polygon overlying … Read more

Can’t install Scipy through pip

After opening up an issue with the SciPy team, we found that you need to upgrade pip with: pip install –upgrade pip And in Python 3 this works: python3 -m pip install –upgrade pip for SciPy to install properly. Why? Because: Older versions of pip have to be told to use wheels, IIRC with –use-wheel. … Read more

NameError: global name ‘unicode’ is not defined – in Python 3

Python 3 renamed the unicode type to str, the old str type has been replaced by bytes. if isinstance(unicode_or_str, str): text = unicode_or_str decoded = False else: text = unicode_or_str.decode(encoding) decoded = True You may want to read the Python 3 porting HOWTO for more such details. There is also Lennart Regebro’s Porting to Python … Read more