Skip to content

Tag: python

Constructing a co-occurrence matrix in python pandas

I know how to do this in R. But, is there any function in pandas that transforms a dataframe to an nxn co-occurrence matrix containing the counts of two aspects co-occurring. For example a matrix df: would yield: Since the matrix is mirrored on the diagonal I guess there would be a way to optimize code. Answe…

Django 1.6 No Category matches the given query

I am newbie in django .. and i dont understand what is reason this 404 error I have Page not found (404) when i try go to link No Category matches the given query. code: models.py: views.py: urls.py Answer Most likely, you don’t have category object with slug that you are specifying in the url. Due to t…

Sorting a list of unix time stamp values

Given a list of UNIX time stamp values: I need the list to be sorted. When I run this, I got [’31-10-2009 13.45′, ’20-09-2009 14.45′, ’12-12-2009 17.00′, ’07-05-2010 20.00′], which is not at all sorted. Can you see what’s wrong? Answer You’re sorting…

How to save captured image to disk, using Pygame

This is my code, which starts the webcam : I want to know how to capture an image and store it to the current directory. Please suggest the changes required. Answer When you call webcam.get_image it returns an RGB Surface. So just call pygame.image.save(), the file type is determined by the extension and defa…

Django Rest Framework File Upload

I am using Django Rest Framework and AngularJs to upload a file. My view file looks like this: As the last line of post method should return all the data, I have several questions: how to check if there is anything in request.FILES? how to serialize file field? how should I use parser? Answer Use the FileUplo…

Get diagonal without using numpy?

I’m trying to get the diagonal from a matrix in Python without using numpy (I really can’t use it). Does someone here knows how to do it? Example of what I want to get: Or like: Until know I’ve tried a lot of stuff but doesn’t work. If direc==1 I need to get the diagonal that goes from…

if x:, vs if x == True, vs if x is True

Apologies if this has been asked before, but I have searched in vain for an answer to my exact question. Basically, with Python 2.7, I have a program running a series of geoprocessing tools, depended on what is reqested via a series of True/False variables that the user adjusts in the script e.g. However, I h…

Get the part of the string matched by regex

In the case of re.search(), is there a way I can get hold of just the part of input string that matches the regex? i.e. I just want the “heeehe” part and not the stuff that comes before it: Answer match.group(0) is the matched string. Demo: You can also omit the argument, 0 is the default.