使用 "to_dict()" 和 "json.dump()" 通过字典将数据帧转换为 JSON



我正在尝试将数据帧转换为特定的JSON格式。我尝试分别使用来自 pandas 和 json 模块的"to_dict(("和"json.dump(("方法来执行此操作,但我无法获得我想要的 JSON 格式。举例说明:

df = pd.DataFrame({
"Location": ["1ST"] * 3 + ["2ND"] * 3,
"Date": ["2019-01", "2019-02", "2019-03"] * 2,
"Category": ["A", "B", "C"] * 2,
"Number": [1, 2, 3, 4, 5, 6]
})
def dataframe_to_dictionary(df, orientation):
dictionary = df.to_dict(orient=orientation)
return dictionary
dict_records = dataframe_to_dictionary(df, "records")
with open("./json_records.json", "w") as json_records:
json.dump(dict_records, json_records, indent=2)
dict_index = dataframe_to_dictionary(df, "index")
with open("./json_index.json", "w") as json_index:
json.dump(dict_index, json_index, indent=2)

当我将"dict_records"转换为 JSON 时,我得到一个形式的数组:

[
{
"Location": "1ST",
"Date": "2019-01",
"Category": "A",
"Number": 1
},
{
"Location": "1ST",
"Date": "2019-02",
"Category": "B",
"Number": 2
},
...
]

而且,当我将"dict_index"转换为 JSON 时,我得到一个形式的对象:

{
"0": {
"Location": "1ST",
"Date": "2019-01",
"Category": "A",
"Number": 1
},
"1": {
"Location": "1ST",
"Date": "2019-02",
"Category": "B",
"Number": 2
}
...
}

但是,我正在尝试获得如下所示的格式(其中键 = 位置和值 = [{}](,如下所示。提前感谢您的帮助。

{
1ST: [
{
"Date": "2019-01",
"Category": "A",
"Number" 1
},
{
"Date": "2019-02",
"Category": "B",
"Number" 2
},
{
"Date": "2019-03",
"Category": "C",
"Number" 3
}
],
2ND: [
{},
{},
{}
]
}

这可以通过分组实现:

gb = df.groupby('Location')
{k: v.drop('Location', axis=1).to_dict(orient='records') for k, v in gb}

最新更新