I am looking to loop through around 10 million rows in a pandas dataframe and add them to an already existing dict of sets.
For example for a dict like this
x = {10: {1, 2, 3, 5}, 12: {6, 7, 8, 9, 10}}
And a dataframe like this:
d = {'ID': [10, 10, 10, 12, 12, 12], 'Another_ID': [1, 4, 6, 6, 7, 13]}
df = pd.DataFrame(data=d)
ID Another_ID
10 1
10 4
10 6
12 6
12 7
12 13
I would like to go through the rows and add the new values that ID "hasnt seen yet." I would like a result like this.
x = {10: {1, 2, 3, 4, 5, 6}, 12: {6, 7, 8, 9, 10, 13}}
I have tried iterating through using a simple function like the following.
for i in df [['ID' , 'Another_ID' ]] .values():
dict[i[0]].add(i[1])
I can manually add in the values by saying the following like this, but cant do it in a loop!
dict[10].add(6)
If anyone knows how to loop through these two pandas columns and add new values to the set, please let me know!
- keep in mind this must be done relatively quickly as there are 10 million rows
Thanks!