我有一个这样的数据框,
df
col1 col2 col3
A 1 2
B 2 3
C 4 5
B 6 7
B 9 10
C 11 12
A 13 14
我需要将其转换为json格式。当我使用to_jon进行转换时,输出是这样的,
{"col1":{"0":"A","1":"B","2":"C","3":"B","4":"B","5":"C","6":"A"},"col2":
{"0":1,"1":2,"2":4,"3":6,"4":9,"5":11,"6":13},"col3":
{"0":2,"1":3,"2":5,"3":7,"4":10,"5":12,"6":14}}
但是我正在寻找的json看起来像
{"A":{"col2":[1,13],"col3":[2,14]}, "B":{"col2":[2,6,9],"col3":[3,7,10]}, "C":{"col2":
[4,11],"col3":[5,12]}}
我正在寻找熊猫快捷方式/ pythonic方法来有效地完成此任务。
答案 0 :(得分:1)
尝试使用groupby+transpose
:
js = df.groupby('col1').agg(list).T.to_json()
#or df.pivot_table(columns='col1',aggfunc=list).to_json()
print(js)
{"A":{"col2":[1,13],"col3":[2,14]},"B":{"col2":[2,6,9],"col3":[3,7,10]},
"C":{"col2":[4,11],"col3":[5,12]}}