我試圖將多個pandas.DataFrame連接在一個集合中保存在一個mongodb中,所有數據框都具有相同的索引/列和我想保存它,只需一個文件,使用to_json()方法。將數據框的所有單元格作爲字符串,它可能是一個好方法。爲了實現這個目標,我想Concat的這樣的dataframes:將兩個熊貓數據幀合併爲一個數據幀「字典型單元格」(pd.Panel已棄用)
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
? 謝謝!
在第一個選項中,第二個操作('groupby')失去了正確的索引,它有沒有辦法修復它? –
從那以後我編輯過。我相信我現在擁有的是優越的。 – piRSquared