【问题标题】:Convert a dataframe to JSON via a dictionary using "to_dict()" and "json.dump()"使用“to_dict()”和“json.dump()”通过字典将数据帧转换为 JSON
【发布时间】:2020-04-07 05:03:51
【问题描述】:

我正在尝试将数据帧转换为特定的 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: [
        {},
        {},
        {}
    ]
}

【问题讨论】:

  • 出于好奇,为什么是to_dict 而不是to_json
  • 嗯...我只是认为先将数据帧转换为字典是有意义的,但现在你让我开始思考了。
  • 除非您出于特定原因需要将其作为dict,否则在您拥有的对象和您想要的输出之间的代码中,这似乎只是一个不必要的复杂步骤。
  • 同意。但是,我很确定 df.to_json() 会让我处于同一个绑定中 - 将平面数据结构转换为嵌套数据结构。

标签: python json pandas dataframe dictionary


【解决方案1】:

这可以通过groupby实现:

gb = df.groupby('Location')

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

【讨论】:

  • 太棒了!谢谢!
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
相关资源
最近更新 更多