Pandas 数据框转 json 列表格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43134637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:18:55  来源:igfitidea点击:

Pandas dataframe to json list format

jsonpandas

提问by tensor

I have large pandas tabular dataframe to convert into JSON. The standard .to_json() functions does not make a compact format for JSON. How to get JSON output forma like this, using pandas only ?

我有大Pandas表格数据框可以转换为 JSON。标准的 .to_json() 函数没有为 JSON 制作紧凑的格式。如何获得这样的 JSON 输出格式,仅使用 Pandas?

{"index": [ 0, 1 ,3 ],
 "col1": [ "250", "1" ,"3" ],
 "col2": [ "250", "1" ,"3" ]
}

This is a much compact format form of JSON for tabular data. (I can do a loop over the rows.... but)

这是用于表格数据的非常紧凑的 JSON 格式。(我可以在行上循环......但是)

采纳答案by jezrael

It seems you need to_dictfirst and then dictto json:

看来你需要to_dictdictjson

df = pd.DataFrame({"index": [ 0, 1 ,3 ],
 "col1": [ "250", "1" ,"3" ],
 "col2": [ "250", "1" ,"3" ]
})
print (df)
  col1 col2  index
0  250  250      0
1    1    1      1
2    3    3      3


print (df.to_dict(orient='list'))
{'col1': ['250', '1', '3'], 'col2': ['250', '1', '3'], 'index': [0, 1, 3]}


import json

print (json.dumps(df.to_dict(orient='list')))
{"col1": ["250", "1", "3"], "col2": ["250", "1", "3"], "index": [0, 1, 3]}

Because it is not implemented yet:

因为它还没有实现

print (df.to_json(orient='list'))

ValueError: Invalid value 'list' for option 'orient'

ValueError: 选项“orient”的值“list”无效

EDIT:

编辑:

If index is not column, add reset_index:

如果索引不是列,请添加reset_index

df = pd.DataFrame({"col1": [250, 1, 3],
                   "col2": [250, 1, 3]})
print (df)
   col1  col2
0   250   250
1     1     1
2     3     3

print (df.reset_index().to_dict(orient='list'))
{'col1': [250, 1, 3], 'index': [0, 1, 2], 'col2': [250, 1, 3]}

回答by pansen

You can use to_dictand json(and add the indexas extra column if required via assign):

您可以使用to_dictjson(并index根据需要添加作为额外的列assign):

import json

df = pd.DataFrame({"col1": [250, 1, 3],
                   "col2": [250, 1, 3]})

json_dict = df.assign(index=df.index).to_dict(orient="list")
print(json.dumps(json_dict))

>>> '{"index": [0, 1, 2], "col1": [250, 1, 3], "col2": [250, 1, 3]}'