【发布时间】:2018-03-17 10:31:50
【问题描述】:
我正在尝试将多个 pandas.DataFrame 保存在一个集合中的 mongodb 中,所有数据帧都有相同的索引/列,我想将它保存在一个文档中,使用 to_json() 方法.将数据框的所有单元格作为字典,这可能是一个好方法。为了实现这一点,我想像这样连接数据框:
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'
预期的解决方案:
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'}
我使用的方法是
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'}
但是pd.Panel 已弃用DeprecationWarning : Panel is deprecated and will be removed in a future version.它有一个只使用pandas 的解决方法吗?
谢谢!
【问题讨论】:
标签: json mongodb pandas dataframe panel