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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[使用“ 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
  }
...
}

但是,我试图获得如下所示的格式(其中key =位置,值= [{}])。预先感谢您的帮助。

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

答案

这可以通过groupby实现:

gb = df.groupby('Location')

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

以上是关于[使用“ to_dict()”和“ json.dump()”通过字典将数据帧转换为JSON的主要内容,如果未能解决你的问题,请参考以下文章

Django - 在模板中格式化/访问 Pandas.to_dict() 数据框

Pandas使用to_dict函数将dataframe转化为字典(dict)格式数据并指定orientation参数生成不同形式的字典

熊猫弃用警告 to_dict()

Pandas to_dict 意外修改浮点数

带有自定义列名的Python pandas.DataFrame.to_dict

将pandas变为字典,apply