Updating a nested dictionary whose root keys match the index of a certain dataframe with said dataframe’s values

Question:

I have a nested dict that is uniform throughout (i.e. each 2nd level dict will have the same keys).

{
  '0': {'a': 1, 'b': 2},
  '1': {'a': 3, 'b': 4},
  '2': {'a': 5, 'b': 6},
}

and the following data frame

    c
0   9 
1   6
2   4

Is there a way (without for loops) to update/map the dict/key-values such that I get

{
  '0': {'a': 1, 'b': 2, 'c': 9},
  '1': {'a': 3, 'b': 4, 'c': 6},
  '2': {'a': 5, 'b': 6, 'c': 4},
}
Asked By: lefe

||

Answers:

Try this

# input
my_dict = {
  '0': {'a': 1, 'b': 2},
  '1': {'a': 3, 'b': 4},
  '2': {'a': 5, 'b': 6},
}
my_df = pd.DataFrame({'c': [9, 6, 4]})
# build df from my_dict
df1 = pd.DataFrame.from_dict(my_dict, orient='index')
# append my_df as a column to df1
df1['c'] = my_df.values
# get dictionary
df1.to_dict('index')

But a simple loop is much more efficient here. I tested on a sample with 1mil entries and the loop is 2x faster.1

for d, c in zip(my_dict.values(), my_df['c']):
    d['c'] = c
my_dict
{'0': {'a': 1, 'b': 2, 'c': 9},
 '1': {'a': 3, 'b': 4, 'c': 6},
 '2': {'a': 5, 'b': 6, 'c': 4}}

1: Constructing a dataframe is expensive, so unless you want a dataframe (and possibly do other computations later), it’s not worth it to construct one for a task such as this one.

Answered By: not a robot
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.