我正在尝试将 pandas 的 dataFrame 转换为 json。
y=pd.read_csv('testx.csv',encoding='utf-8')
y.columns = ['i','city','language','words']
del y['i']
y = y.set_index(['city','language'])
z=y.to_json(orient='index')
我有不正确的 json[
和{
引号。我究竟做错了什么?
{"["Moscow","Russian"]":{"words":3300000},"["Moscow","English"]":{"words":550000},"["Moscow","French"]":{"words":100000},"
["London","English"]":{"words":9100000},"["London","Russian"]":{"words":150000},"["London","Spanish"]":{"words":90000},...
理想情况下,数据框:
city language words
Moscow Russian 3300000
Moscow English 550000
Moscow French 100000
London English 9100000
London Russian 150000
London Spanish 90000
...
必须转换为:
[
{
"city": "Moscow",
"language": {
"Russian": 3300000,
"English": 550000,
"French": 100000
}
},
{
"city": "London",
"language": {
"English": 9100000,
"Russian": 150000,
"Spanish": 90000
}
}
]