How can I call column in my code using its index in dataframe instead of its name. For example I have dataframe df with columns a, b, c Instead of calling df[‘a’], can I call it using its column index like df[1]? Answer You can use iloc: Example:
Django: Use firstof of if-else block inside blocktrans
I have two variables var1 and var2. I want to do this, It gives me error that ‘blocktrans’ doesn’t allow other block tags. Because we are not allowed to use any other tag inside blocktrans, what is the solution of this kind of problem? Answer From django 1.9 onwards, you can use firstof to assign result to context. See django-docs
Django admin is_staff based on group
Is it possible to have is_staff selected by choosing a group? Let’s say there are two groups: users, admins When a new user is in the users group he is not staff, but if he is in the admins group he is staff. Answer I managed to make it work by extending the UserAdmin class and in the get_form function
Matplotlib plots aren’t shown when running file from bash terminal
Plots are normally shown when I run files from the ipython shell or from an ipython notebook, but they don’t show up when I run the file from a bash terminal — everything else works fine when is run from a bash terminal. Sample python script (trial.py): This is what I get (plot doesn’t show up): If I do before
Attribute error while using opencv for face recognition
I am teaching myself how to use openCV by writing a simple face recognition program I found on youtube. I have installed opencv version 2 as well as numpy 1.8.0. I am using python 2.7. I copyed this code exactly how it was done in the video and article links below, yet I keep getting errors. AttributeError: ‘module’ object has
Why does fftfreq produce negative values?
From the documentation for fftfreq: Why are there negative values in the output array? I am trying to produce a plot of amplitude vs frequency. I can get the amplitude by running the abs() function over the elements of fourier, but how do I convert freq into a series of frequencies that I can use as an x-axis when plotting
Dealing with masked coordinate arrays in pcolormesh
I’m working on visualizing some climate model output. The computation is done on a projected latitude/longitude grid. Since the model is simulating sea ice, all land grid cells are masked. The standard tools for plotting geographical information in Python are Basemap and Cartopy, both of which use matplotlib routines. In particular, pcolormesh is the obvious choice for plotting. If there
Python Unittest and object initialization
My Python version is 3.5.1 I have a simple code (tests.py): If I run it with command ‘python tests.py’ I will get the results: Why it is happening? And how to fix it. I expect that each tests run will be independent (each test should pass), but it is not as we can see. Answer The array is shared by
Boto3: grabbing only selected objects from the S3 resource
I can grab and read all the objects in my AWS S3 bucket via and then would give me the path within the bucket. Is there a way to filter beforehand for only those files respecting a certain starting path (a directory in the bucket) so that I’d avoid looping over all the objects and filtering later? Answer Use the
Replace values in one dataframe with values in second dataframe in Python
I have a large dataframe (DF1) that contains a variable containing UK postcode data. Inevitably there are some typos in the data. However, after some work with regular expressions, I have created a second database that contains corrected versions of the postcode data (but only for those rows where the original postcode was incorrect) – DF2. (N.B. the index values