使用熊猫从嵌套json转换为csv

时间:2019-07-04 08:47:10

标签: python json pandas csv nested

我正在尝试将嵌套的json转换为csv文件,但是我在为文件结构所需的逻辑而苦苦挣扎:这是一个包含2个对象的json,我想仅将其中一个转换为csv,这是带有嵌套的列表。

我在this blog post中发现了非常有用的“扁平化” json信息。我基本上已经在适应它来解决我的问题,但是它仍然对我不起作用。

我的json文件如下:

{
  "tickets":[
    {
      "Name": "Liam",
      "Location": {
        "City": "Los Angeles",
        "State": "CA"
      },
      "hobbies": [
        "Piano",
        "Sports"
      ],
      "year" : 1985,
      "teamId" : "ATL",
      "playerId" : "barkele01",
      "salary" : 870000
    },
    {
      "Name": "John",
      "Location": {
        "City": "Los Angeles",
        "State": "CA"
      },
      "hobbies": [
        "Music",
        "Running"
      ],
      "year" : 1985,
      "teamId" : "ATL",
      "playerId" : "bedrost01",
      "salary" : 550000
    }
  ],
  "count": 2
}

到目前为止,我的代码如下:

import json
from pandas.io.json import json_normalize
import argparse


def flatten_json(y):
    out = {}

    def flatten(x, name=''):
        if type(x) is dict:
            for a in x:
                flatten(x[a], name + a + '_')
        elif type(x) is list:
            i = 0
            for a in x:
                flatten(a, name + str(i) + '_')
                i += 1
        else:
            out[name[:-1]] = x
    flatten(y)
    return out


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Converting json files into csv for Tableau processing')
    parser.add_argument(
        "-j", "--json", dest="json_file", help="PATH/TO/json file to convert", metavar="FILE", required=True)

    args = parser.parse_args()

    with open(args.json_file, "r") as inputFile:  # open json file
        json_data = json.loads(inputFile.read())  # load json content
    flat_json = flatten_json(json_data)
    # normalizing flat json
    final_data = json_normalize(flat_json)

    with open(args.json_file.replace(".json", ".csv"), "w") as outputFile:  # open csv file

        # saving DataFrame to csv
        final_data.to_csv(outputFile, encoding='utf8', index=False)

我想获得的是在csv中每张票有1行,标题为:

Name,Location_City,Location_State,Hobbies_0,Hobbies_1,Year,TeamId,PlayerId,Salary

我真的很感激可以做点击的事情! 谢谢!

3 个答案:

答案 0 :(得分:1)

可能有一个更简单的解决方案。但这应该可行!

import json
import pandas as pd

with open('file.json') as file:
    data = json.load(file)

df = pd.DataFrame(data['tickets'])

for i,item in enumerate(df['Location']):
    df['location_city'] = dict(df['Location'])[i]['City']
    df['location_state'] = dict(df['Location'])[i]['State']

for i,item in enumerate(df['hobbies']):
    df['hobbies_{}'.format(i)] = dict(df['hobbies'])[i]

df = df.drop({'Location','hobbies'}, axis=1)

print(df)

output

答案 1 :(得分:1)

您已经具有拉平Json对象的功能,只需拉平票证即可:

...
with open(args.json_file, "r") as inputFile:  # open json file
    json_data = json.loads(inputFile.read())  # load json content
final_data = pd.DataFrame([flatten_json(elt) for elt in json_data['tickets']])
...

使用示例数据,final_data符合预期:

  Location_City Location_State  Name hobbies_0 hobbies_1   playerId  salary teamId  year
0   Los Angeles             CA  Liam     Piano    Sports  barkele01  870000    ATL  1985
1   Los Angeles             CA  John     Music   Running  bedrost01  550000    ATL  1985

答案 2 :(得分:1)

实际上我最近写了一个名为cherrypicker的程序包来处理这种确切的事情,因为我不得不经常这样做!

我认为以下代码将为您提供确切的帮助:

from cherrypicker import CherryPicker
import json
import pandas as pd

with open('file.json') as file:
    data = json.load(file)

picker = CherryPicker(data)
flat = picker['tickets'].flatten().get()
df = pd.DataFrame(flat)
print(df)

这给了我输出:

  Location_City Location_State  Name hobbies_0 hobbies_1   playerId  salary teamId  year
0   Los Angeles             CA  Liam     Piano    Sports  barkele01  870000    ATL  1985
1   Los Angeles             CA  John     Music   Running  bedrost01  550000    ATL  1985

您可以通过以下方式安装软件包:

pip install cherrypicker

... https://cherrypicker.readthedocs.io上有更多文档和指南。