Let’s say I have the DataFrame I want to create a dictionary in the form Solutions I have found deal with the case of creating a dict with single values using something like Answer Set ‘filename’ as the index, take the transpose, then use to_dict with orient=’list’: The resulting…
Tag: dictionary
I need a way to map multiple keys to the same value in a dictionary
Admittedly, this question seems like it might be a popular one, but I couldn’t really find it (perhaps I wasn’t using the right search terms). Anyway, I need something of this sort: So whenever a key’s value is already in the dict, append the key to the list of keys mapping to that value; el…
Converting dictionary with known indices to a multidimensional array
I have a dictionary with entries labelled as {(k,i): value, …}. I now want to convert this dictionary into a 2d array where the value given for an element of the array at position [k,i] is the value from the dictionary with label (k,i). The length of the rows will not necessarily be of the same size (e.…
subclassing dict; dict.update returns incorrrect value – python bug?
I needed to make a class that extended dict and ran into an interesting problem illustrated by the dumb example in the image below. Why is d.update() ignoring the class’s __getitem__? EDIT: This is in python2.7 which does not appear to contain collections.UserDict Thinking UserDict.UserDict is the equiv…
Python. How to subtract 2 dictionaries
I have 2 dictionaries, A and B. A has 700000 key-value pairs and B has 560000 key-values pairs. All key-value pairs from B are present in A, but some keys in A are duplicates with different values and some have duplicated values but unique keys. I would like to subtract B from A, so I can get the remaining 14…
Remove duplicate JSON objects from list in python
I have a list of dict where a particular value is repeated multiple times, and I would like to remove the duplicate values. My list: function to remove duplicate values: When I call this function I get generator object. When I try to iterate over the generator I get TypeError: unhashable type: ‘dictR…
How do I find the keys in one dictionary that do not have a counterpart in another dictionary?
In Python, how do I find the keys in one dictionary that do not have a counterpart in another dictionary? The practical problem is that I have a dictionary of people that enrolled and a dictionary with their daily participation and I am trying to find the people that enrolled but did not participate, or are i…
Pandas dataframe from nested dictionary
My dictionary looks like this: I want to get a dataframe that looks like this: I tried calling pandas.from_dict(), but it did not give me the desired result. So, what is the most elegant, practical way to achieve this? EDIT: In reality, my dictionary is of depth 4, so I’d like to see a solution for that…
Adding Multiple Values to Single Key in Python Dictionary Comprehension
I wanted to learn how to use dictionary comprehension and decided to use one for the previously solved task. I need to assign multiple values to the same key. I was wondering if there’s a better way to achieve what I’m trying to do than with the code I’ve written so far. For example I have t…
Django – When button is clicked I want to change the dictionary content
On my html page I have a button, and a number being displayed. I want the number to increase by 1 every time the button is pressed. This is the code that I have so far, but for some it doesn’t work. mypage.html views.py Answer On every request a new instance of the python script is run which would resul…