0

我正在尝试将 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
    }
  }
]
4

1 回答 1

0

你愿意使用 dict 理解来明确地制作你的输出吗?然后,您可以使用json.dumps从 python dict 转换为 JSON。不幸的是,您想要的形式超出了 to_json 支持的标准输出。

[{"city":i, 
  "language":{l:n 
      for l,n in zip(g['language'], g['words'])}} 
   for i,g in df.groupby('city')] 
[{'city': 'London',
  'language': {'English': 9100000, 'Russian': 150000, 'Spanish': 90000}},
 {'city': 'Moscow',
  'language': {'English': 550000, 'French': 100000, 'Russian': 3300000}}]
于 2016-03-21T05:26:37.527 回答