concat two pandas DataFrame with same column/index into one DataFrame

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

I'm trying to concat multiples pandas.DataFrame to be saved in a mongodb in just one collection, all the dataframes have the same index/columns and I wanted to save it, in just one document, using to_json() method. Having all the cells of the dataframe as dicts, its probably a good approach. To accomplish that I wanted to concat the dataframes like this:

df1:                  index   A      B  1     'A1'   'B1'  2     'A2'   'B2'  3     'A3'   'B3'  df2:  index  A      B  1    'a1'   'b1'  2    'a2'   'b2'  3    'a3'   'b3' 

Expected solution:

df_sol:  index    A                    B  1        {d1:'A1', d2:'a1'}   {d1:'B1', d2:'b1'}  2        {d1:'A2', d2:'a2'}   {d1:'B2', d2:'b2'}  3        {d1:'A3', d2:'a3'}   {d1:'B3', d2:'b3'} 

is there a way to accomplish that without using an iterator?, any guidance will be appreciate!. Thanks!

回答1:

Use pd.Panel with apply + pd.Series.to_dict

pd.Panel(dict(d1=df1, d2=df2)).apply(pd.Series.to_dict, 0)                                A                         B index                                                     1      {'d1': 'A1', 'd2': 'a1'}  {'d1': 'B1', 'd2': 'b1'} 2      {'d1': 'A2', 'd2': 'a2'}  {'d1': 'B2', 'd2': 'b2'} 3      {'d1': 'A3', 'd2': 'a3'}  {'d1': 'B3', 'd2': 'b3'} 

This is assuming the columns you have labeled index are in fact the index. Otherwise, make sure they are:

df1 = df1.set_index('index') df2 = df2.set_index('index') 


回答2:

Let's try this bit of code:

df1 = df1.set_index('index') df2 = df2.set_index('index')  df_int = pd.merge(df1,df2,left_index=True, right_index=True,suffixes=('_d1','_d2'))  A_dict = df_int[['A_d1','A_d2']].rename(columns={'A_d1':'d1','A_d2':'d2'}).to_dict(orient='records') B_dict = df_int[['B_d1','B_d2']].rename(columns={'B_d1':'d1','B_d2':'d2'}).to_dict(orient='records')  df_sol = pd.DataFrame({'A':A_dict,'B':B_dict})  df_sol 

Output:

                          A                         B 0  {'d1': 'A1', 'd2': 'a1'}  {'d1': 'B1', 'd2': 'b1'} 1  {'d1': 'A2', 'd2': 'a2'}  {'d1': 'B2', 'd2': 'b2'} 2  {'d1': 'A3', 'd2': 'a3'}  {'d1': 'B3', 'd2': 'b3'} 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!