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
Pandas dataframe to json list format
提问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_dict
first and then dict
to json
:
看来你需要to_dict
先dict
于json
:
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_dict
and json
(and add the index
as extra column if required via assign
):
您可以使用to_dict
和json
(并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]}'