Skip to content
Advertisement

using a loop to insert values from a column from a data frame into a dictionary key

There are around 60.000 dictionaries stored in a list. There is also a dataframe with the same amount of rows of which I want to take one column and insert into the dictionaries as a key value pair. I have created a for loop which is supposed to update dictionary values, which however seems to take forever. I am looking for a more optimal way to succeed considering the amount of rows.

JavaScript

Advertisement

Answer

Due to the nested loop and based on the information you’ve given you are doing 60,000 x 60,000 = 3,600,000,000 dictionary updates, most of them in vain because you’re overriding each update 59,999 times.

So I suspect you have the following situation: A dataframe df and a list of dictionaries list_of_dicts that have the same length (length of df = number of rows), for instance:

JavaScript

Most likely you’re trying to do:

JavaScript

Now this gives you the following new_dicties

JavaScript

but list_of_dicts looks the same, because the variables are references (pointers, if you will).

If that’s fine, then you could also just stick with the original list_of_dicts and do

JavaScript

If that’s not what you want, then you could do either

JavaScript

in case you have Python 3.9 or higher or

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement