使用python将csv转换为Json

时间:2021-05-18 14:25:01

标签: python json pandas

我有一个包含数据的 csv 文件,我想将其转换为 json 格式,但我遇到了有关格式的问题。

csv 文件中的数据输入:

enter image description here

Dataframe 中显示的数据: enter image description here

我想要的json数据格式:

[restaurant name: 
  Address:
  Sate:
  Logo:
  Review:
    menu: {
    food name
    food price
    
    },
    {
    food name
    food price
    
    },
]

我已经尝试过这段代码,但没有得到想要的结果。

代码如下:

import pandas as pd

df = pd.read_csv(r'.\test.csv')
print(df)

df.to_json(r'.\test.json', orient='values')

有没有办法得到那种格式的结果?

编辑:用于说明的最小数据集

Restaurant_Name,Address,State,Logo,Review,Menu,Price
"Taiwan bubble tea","Taipei,Taiwan","bestille","http://url",6,"Classic milk tea","47kr"
"Taiwan bubble tea","Taipei,Taiwan","bestille","http://url",6,"Matcha Mlik tea","47kr"
"Taiwan bubble tea","Taipei,Taiwan","bestille","http://url",6,"Melon Milk tea","47kr"
"Taiwan bubble tea","Taipei,Taiwan","bestille","http://url",6,"Mango tea","47kr" 

1 个答案:

答案 0 :(得分:1)

你可以用

(df
    .rename(columns={"Menu":"food_name"}) # change column name to have "food_name" instead of "Menu" in the resulting nested JSON
    .groupby(["Restaurant_Name", "Address", "State", "Logo", "Review"])
    .apply(lambda x: x[["food_name", "Price"]].to_dict('records'))
    .reset_index()
    .rename(columns={0:"Menu"})
    .to_json(r"yourJSONfile.json", orient="records", indent=4))
  1. 首先将数据groupby转化为餐厅的特征(名称、地址、状态、徽标和评论),对于每家餐厅,我们将所有行转换为包含信息的字典数组关于食物

  2. 重置索引后,您将获得一个数据框,每个餐厅一行,最后一列名为 0,并包含菜单项及其价格的数组。

  3. 因为它更漂亮而且更有意义,我们将第 0 列重命名为“Menu”

  4. 带有 orient='records' 的函数 to_json 然后可以将数据帧写入 JSON 文件,作为一个数组,每个餐厅有一个条目(= 数据帧的 1 行),每个键是一个列名。特别是,“菜单”键将包含一系列食物。

结果如下:

enter image description here