将 Pandas DataFrame 转换为 JSON 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39257147/
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
Convert Pandas DataFrame to JSON format
提问by user3447653
I have a Pandas DataFramewith two columns – one with the filename and one with the hour in which it was generated:
我有一个DataFrame有两列的 Pandas——一列是文件名,另一列是生成时间:
File Hour
F1 1
F1 2
F2 1
F3 1
I am trying to convert it to a JSON file with the following format:
我正在尝试将其转换为具有以下格式的 JSON 文件:
{"File":"F1","Hour":"1"}
{"File":"F1","Hour":"2"}
{"File":"F2","Hour":"1"}
{"File":"F3","Hour":"1"}
When I use the command DataFrame.to_json(orient = "records"), I get the records in the below format:
当我使用命令时DataFrame.to_json(orient = "records"),我得到以下格式的记录:
[{"File":"F1","Hour":"1"},
{"File":"F1","Hour":"2"},
{"File":"F2","Hour":"1"},
{"File":"F3","Hour":"1"}]
I'm just wondering whether there is an option to get the JSON file in the desired format. Any help would be appreciated.
我只是想知道是否可以选择以所需格式获取 JSON 文件。任何帮助,将不胜感激。
回答by Nickil Maveli
The output that you get after DF.to_jsonis a string. So, you can simply slice it according to your requirement and remove the commas from it too.
你得到的输出DF.to_json是一个string. 因此,您可以简单地根据您的要求对其进行切片并从中删除逗号。
out = df.to_json(orient='records')[1:-1].replace('},{', '} {')
To write the output to a text file, you could do:
要将输出写入文本文件,您可以执行以下操作:
with open('file_name.txt', 'w') as f:
f.write(out)
回答by Brad Solomon
In newer versions of pandas (0.20.0+, I believe), this can be done directly:
在较新版本的熊猫(0.20.0+,我相信)中,这可以直接完成:
df.to_json('temp.json', orient='records', lines=True)
Direct compression is also possible:
也可以直接压缩:
df.to_json('temp.json.gz', orient='records', lines=True, compression='gzip')
回答by sagarsar
I think what the OP is looking for is:
我认为 OP 正在寻找的是:
with open('temp.json', 'w') as f:
f.write(df.to_json(orient='records', lines=True))
This should do the trick.
这应该可以解决问题。
回答by J Rishabh Kumar
instead of using dataframe.to_json(orient = “records”)use dataframe.to_json(orient = “index”)my above code convert the dataframe into json format of dict like {index -> {column -> value}}
而不是dataframe.to_json(orient = “records”)使用dataframe.to_json(orient = “index”)我上面的代码将数据帧转换为 dict 的 json 格式,如 {index -> {column -> value}}
回答by Miguel Gomez
To transform a dataFrame in a real json (not a string) I use:
要在真正的 json(不是字符串)中转换数据帧,我使用:
from io import StringIO
import json
import DataFrame
buff=StringIO()
#df is your DataFrame
df.to_json(path_or_buf=buff,orient='records')
dfJson=json.loads(buff)
回答by Siva
Here is small utility class that converts JSON to DataFrame and back: Hope you find this helpful.
这是将 JSON 转换为 DataFrame 并返回的小型实用程序类:希望您觉得这有帮助。
# -*- coding: utf-8 -*-
from pandas.io.json import json_normalize
class DFConverter:
#Converts the input JSON to a DataFrame
def convertToDF(self,dfJSON):
return(json_normalize(dfJSON))
#Converts the input DataFrame to JSON
def convertToJSON(self, df):
resultJSON = df.to_json(orient='records')
return(resultJSON)
回答by Hafiz Shehbaz Ali
convert data-frame to list of dictionary
将数据框转换为字典列表
list_dict = []
for index, row in list(df.iterrows()):
list_dict.append(dict(row))
save file
保存存档
with open("output.json", mode) as f:
f.write("\n".join(str(item) for item in list_dict))

