简体   繁体   中英

Combine two pandas dataframes of dictionaries

I'd like to take 2 separate dataframes, each comprised of a bunch of dictionaries, and combine them to yield the following:

df1 = pd.DataFrame([[{'a':1}, {'a':2}]])
df2 = pd.DataFrame([[{'b':1}, {'b':2}]])
df3 = pd.some_function(df1, df2)

where pd.some_function takes the two dfs and performs a cell-wise merge of the dictionaries:

                    0                   1
0  {u'a': 1, u'b': 1}  {u'a': 2, u'b': 2}

I know I can do this with a for loop, but is there a pandas function that can do this more succinctly? Simply adding the dfs does not work. I'm familiar with df.applymap , but my understanding is that will apply a function to each cell of a single df.

By using concat + ChainMap

from collections import ChainMap


df=pd.concat([df1,df2])
df.groupby(df.index)[0,1].agg(lambda x :dict(ChainMap(*x.values.tolist())))
Out[94]: 
                  0                 1
0  {'b': 1, 'a': 1}  {'b': 2, 'a': 2}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM