数据框到json,东方索引和索引等于行值

时间:2017-07-11 03:32:57

标签: python json pandas

我有一个pandas数据框,我试图转换为某种json格式:

df = pd.DataFrame([['A',1,2,3],['B',2,3,4],['C','C',1,6],['D','D',9,7]], columns=['W','X','Y','Z'])

df.set_index('W', inplace=True, drop=True, append=False)

df
   X  Y  Z
W
A  1  2  3
B  2  3  4
C  C  1  6
D  D  9  7

我希望得到一个json输出如下:

output_json = {'A': {'X':1,'Y':2,'Z':3}, 'B': {'X':2,'Y':3,'Z':4}, 'C':{'Y':1,'Z':6}, 'D': {'Y':9,'Z':7} }

这是我尝试的但是我无法获得'C'和'D'键的预期结果:

df.to_json(orient='index')
'{"A":{"X":1,"Y":2,"Z":3},"B":{"X":2,"Y":3,"Z":4},"C":{"X":"C","Y":1,"Z":6},"D":{"X":"D","Y":9,"Z":7}}'

如何解决这个问题?也许这是我想念的直截了当的事情。感谢。

1 个答案:

答案 0 :(得分:1)

您可以先转换to_dict,然后使用嵌套字典理解仅过滤int值,最后使用json dumps

import json

d = df.to_dict(orient='index')
j = json.dumps({k:{x:y for x,y in v.items() if isinstance(y, int)} for k, v in d.items()})
print (j)
{"A": {"X": 1, "Y": 2, "Z": 3}, 
 "C": {"Y": 1, "Z": 6},
 "D": {"Y": 9, "Z": 7},
 "B": {"X": 2, "Y": 3, "Z": 4}}