将数据帧转换为json文件

时间:2019-04-12 11:07:02

标签: python json pandas

ERROR
enter image description here我有一个已使用pd.dummies编码的数据框。 我要为此制作一个JSON文件。

我已通过以下代码将其转换为json,但是我也想要标题。

export_file = dropped.to_json (r'file_path.json', orient='records')

预期:

{"headers": [ "married","never married","divorced","seperated","unknown"], "data":["10000","01000","00100","00010","00001"]} 

实际:

"SEPARATED":0,"UNKNOWN":0,"WIDOWED":0},{"DIVORCED":0,"MARRIED":0,"NEVER MARRIED":0,"SEPARATED":0,"UNKNOWN":0,"WIDOWED":1},{"DIVORCED":0,"MARRIED":0,"NEVER MARRIED":0,"SEPARATED":0,"UNKNOWN":1,"WIDOWED":0},{"DIVORCED":0,"MARRIED":0,"NEVER MARRIED":0,"SEPARATED":1,"UNKNOWN":0,"WIDOWED":0}]

enter image description here

1 个答案:

答案 0 :(得分:0)

使用:

#sample  data
a = ["married|divorced|unknown","never married|married","divorced","seperated|unknown"]
df = pd.Series(a).str.get_dummies()
print (df)
   divorced  married  never married  seperated  unknown
0         1        1              0          0        1
1         0        1              1          0        0
2         1        0              0          0        0
3         0        0              0          1        1

手动创建dictionary,数据加入列表推导,然后转换为json

d = {"headers": df.columns.tolist(), 'data':[''.join(x) for x in df.values.T.astype(str)]}
print (d)
{'headers': ['divorced', 'married', 'never married', 'seperated', 'unknown'], 
 'data': ['1010', '1100', '0100', '0001', '1001']}

j = json.dumps(d)
print (j)
{"headers": ["divorced", "married", "never married", "seperated", "unknown"], 
 "data": ["1010", "1100", "0100", "0001", "1001"]}

import json
with open('data.json', 'w') as outfile:
    json.dump(d, outfile)