Working with Django 1.4 I would like to catch an Integrity Error when a user does enter a duplicate value. At this moment the application just shows the default Django error page. (debug = True) model admin form At this moment I get an error page showing a ValidationError instead of an IntegrityError. (Which …
Tag: python
Pandas Dataframe datetime slicing with Index vs MultiIndex
With single indexed dataframe I can do the following: Date time slicing works when you give it a complete day (i.e. 2016-01-01), and it also works when you give it a partial date, like just the year and month (2016-01). All this works great, but when you introduce a multiindex, it only works for complete date…
How to divide two columns element-wise in a pandas dataframe
I have two columns in my pandas dataframe. I’d like to divide column A by column B, value by value, and show it as follows: The columns: And the expected result: How do I do this? Answer Just divide the columns:
How to get the dependency tree with spaCy?
I have been trying to find how to get the dependency tree with spaCy but I can’t find anything on how to get the tree, only on how to navigate the tree. Answer It turns out, the tree is available through the tokens in a document. Would you want to find the root of the tree, you can just go
pandas multiple conditions based on multiple columns
I am trying to color points of a pandas dataframe depending on TWO conditions. Example: I have tried so many different ways now and everything I found online was only depending on one condition. My example code always raises the Error: Here’s the code. Tried several variations without success. Btw: I un…
Splitting a Python list into a list of overlapping chunks
This question is similar to Slicing a list into a list of sub-lists, but in my case I want to include the last element of each previous sub-list as the first element in the next sub-list. And I have to take into account that the last sub-list always has to have at least two elements. For example: The result f…
Random 32 hexadecimal digits in Python
I am trying to find a great way to produce a 32 digit hex sequence that is random and gets its randomness from a Big Number like 10*78. For Python code I also found this: This produces a 64 digit hex string, BUT sometimes the result is 63 or even 61 characters and I’m not sure why? I need it
How to set a tkinter window to a constant size
I’m programming a little game with tkinter and briefly, I’m stuck. I have a kind od starting menu, in which are two buttons and one label. If I just create the frame everything is fine, it has the size 500×500 pixels I want the background not to change when I create the buttons and the labe, …
“Syntax error: Unterminated quoted string” error when calling a bash script from Python
I have a situation where, I need to call a bash script inside a python script which is in turn called inside another python script. download-output-files.py: watcher.py: copy_output_file.sh: When I run download-output-files.py , it calls watcher.py , which in turn calls copy_output_file.sh and below is the er…
How to get rid of “Unnamed: 0” column in a pandas DataFrame read in from CSV file?
I have a situation wherein sometimes when I read a csv from df I get an unwanted index-like column named unnamed:0. file.csv The CSV is read with this: This is very annoying! Does anyone have an idea on how to get rid of this? Answer It’s the index column, pass pd.to_csv(…, index=False) to not wri…