转换熊猫数据框架为 JSON 格式

我有一个熊猫 DataFrame,它有两栏——一栏是文件名,另一栏是生成时间:

 File       Hour
F1         1
F1         2
F2         1
F3         1

我试图将其转换为 JSON 文件,格式如下:

{"File":"F1","Hour":"1"}
{"File":"F1","Hour":"2"}
{"File":"F2","Hour":"1"}
{"File":"F3","Hour":"1"}

当我使用 DataFrame.to_json(orient = "records")命令时,我会得到以下格式的记录:

[{"File":"F1","Hour":"1"},
{"File":"F1","Hour":"2"},
{"File":"F2","Hour":"1"},
{"File":"F3","Hour":"1"}]

我只是想知道是否有一个选项来获得所需格式的 JSON 文件。如果你能帮忙,我将不胜感激。

217650 次浏览

The output that you get after DF.to_json is a string. So, you can simply slice it according to your requirement and remove the commas from it too.

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)

I think what the OP is looking for is:

with open('temp.json', 'w') as f:
f.write(df.to_json(orient='records', lines=True))

This should do the trick.

In newer versions of pandas (0.20.0+, I believe), this can be done directly:

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')

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}}

To transform a dataFrame in a real json (not a string) I use:

    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)

Here is small utility class that converts JSON to DataFrame and back: Hope you find this helpful.

# -*- 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)

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))

Try this one:

json.dumps(json.loads(df.to_json(orient="records")))

use this formula to convert a pandas DataFrame to a list of dictionaries :

import json
json_list = json.loads(json.dumps(list(DataFrame.T.to_dict().values())))