I’m Looking for a generic way of turning a DataFrame to a nested dictionary This is a sample data frame The number of columns may differ and so does the column names. like this : What is best way to achieve this ? closest I got was with the zip function but haven’t managed to make it work for more
Tag: python
Django REST Framework serializer field required=false
from the documentation: read_only Set this to True to ensure that the field is used when serializing a representation, but is not used when updating an instance during deserialization. Defaults to False required Normally an error will be raised if a field is not supplied during deserialization. Set to false i…
Python: launch default mail client on the system
I’m fairly new to Python and I’m trying to write a plugin for a text editor. I want to know if there is a way to launch default system E-Mail client from python code. Answer With pywin32: Update Ah, I misread your question and presumed you’re on Win platform. A platform independent solution …
Django REST Framework – Separate permissions per methods
I am writing an API using Django REST Framework and I am wondering if can specify permissions per method when using class based views. Reading the documentation I see that is quite easy to do if you are writing function based views, just using the @permission_classes decorator over the function of the views y…
django, “is not JSON serializable” when using ugettext_lazy?
I have this in my views.py Since I start using this import: from django.utils.translation import ugettext_lazy as _ at this line: message = _(‘This is a test message’) I get this error: Why? What am I doing wrong? Answer You can also create you own JSON encoder that will force __proxy__ to unicode…
How to save the Pandas dataframe/series data as a figure?
It sounds somewhat weird, but I need to save the Pandas console output string to png pics. For example: Is there any way like df.output_as_png(filename=’df_data.png’) to generate a pic file which just display above content inside? Answer Option-1: use matplotlib table functionality, with some addi…
Using len for text but discarding spaces in the count
So, I am trying to create a program which counts the number of characters in a string which the user inputs, but I want to discard any spaces that the user enters. Using this, I can get the number of the characters in each word, without spaces, but I don’t know how to add each number together and print …
How to check if a string is a valid regex in Python?
In Java, I could use the following function to check if a string is a valid regex (source): Is there a Python equivalent of the Pattern.compile() and PatternSyntaxException? If so, what is it? Answer Similar to Java. Use re.error exception: exception re.error Exception raised when a string passed to one of th…
Is there a way to clear your printed text in python?
I have wanted for a long time to find out how to clear something like print(“example”) in python, but I cant seem to find anyway or figure anything out. Now I need to clear it, and write some new text. It would print. Hey But I want to clear the “Hey” so the user shouldnt look at both …
Django-queryset join without foreignkey
model.py What I want is to get id_noga where dzienrok=1234. I know that dziens should be but it isn’t and I can’t change that. Normally I would use something like but I don’t know how to join and filter those tables without foreignkey. Answer It’s possible to join two tables by perform…