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

时间:2019-12-13 18:04:43

标签: python json pandas dataframe dictionary

我正在尝试将数据框转换为特定的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 = location和values = [{}])。预先感谢您的帮助。

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

1 个答案:

答案 0 :(得分:1)

这可以通过groupby实现:

gb = df.groupby('Location')

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