Let us consider a pandas dataframe df
containing dictionaries in one of its columns (column mydict
):
<code> mystring mydict0 a {'key1': 'value1'}1 a {'key2': 'value2'}2 b {'key2': 'value2'}</code><code> mystring mydict 0 a {'key1': 'value1'} 1 a {'key2': 'value2'} 2 b {'key2': 'value2'} </code>mystring mydict 0 a {'key1': 'value1'} 1 a {'key2': 'value2'} 2 b {'key2': 'value2'}
I would like to “merge” the dictionaries as part of a groupby operation, e.g. df.groupby('mystring')['mydict'].apply(lambda x: merge_dictionaries(x))
.
The expected output for mystring
‘a’ would be: {'key1': 'value1', 'key2': 'value2'}
The usual way of combining dictionaries is to update an existing dictionary (dict1.update(dict2)
), so I am not sure how to implement this here.
Here is the code snippet to create the dataframe used in this example:
mycolumns = ['mystring', 'mydict']
df = pd.DataFrame(columns = mycolumns)
df = df.append(pd.DataFrame(['a', {'key1':'value1'}],mycolumns).T)
df = df.append(pd.DataFrame(['a', {'key2':'value2'}],mycolumns).T)
df = df.append(pd.DataFrame(['b', {'key3':'value3'}],mycolumns).T)
df = df.reset_index(drop=True)