将Pivot Like数据转换为JSON使用Python或Pandas

huangapple go评论122阅读模式
英文:

Convert Pivot Like Data into JSON using Python or Pandas

问题

以下是要翻译的内容:

我是Python的新手,我在将一些CSV数据转换为JSON数据时遇到了问题。

所以这是CSV中的数据

原始数据

将Pivot Like数据转换为JSON使用Python或Pandas

编辑1: 示例Excel文件链接

数据透视表

这些数据的数据透视表如下所示。基本上它对配置文件进行分组,并列出与之关联的所有ID

将Pivot Like数据转换为JSON使用Python或Pandas

我尝试使用Python和pandas将上述数据转换为以下格式:

[
    {
        "profile": "SG-1234-BOM-A",
        "ids": ["8695e561-....639", "a65744ae-....9cb54"]
    },
    {
        "profile": "SG-4567-PUN02",
        "ids": ["6ee0f559-....96b08"]
    },
    {
        "profile": "SG-7527-DEL02",
        "ids": ["6f2c.....62ba", "78f2.....fc939", "8885....0dc"]
    }
]

我也尝试使用以下代码:

gp = df.groupby(['profile']).apply(lambda x: x.to_json(orient='records')).to_dict()

无论我尝试什么都没有成功。我有10000多行需要以JSON格式进行格式化。在此先感谢任何帮助!

英文:

I am a newbie to python and I have an issue with converting some csv data to json data.

So here is the data in csv

RAW Data

将Pivot Like数据转换为JSON使用Python或Pandas

EDIT 1: Link to sample excel file

Pivot Data

Pivot of this data translates like this. Basically it groups the profile and list all ids associated with it

将Pivot Like数据转换为JSON使用Python或Pandas

I am trying to use python and pandas to convert the above data into following format:

   [
    {
        "profile": "SG-1234-BOM-A",
        "ids": ["8695e561-....639", "a65744ae-....9cb54"]
    },
    {
        "profile": "SG-4567-PUN02",
        "ids": ["6ee0f559-....96b08"]   
    },
    {
        "profile": "SG-7527-DEL02",
        "ids": ["6f2c.....62ba", "78f2.....fc939", "8885....0dc"]   
    }
   ]

I have tried to use following codes as well:

gp = df.groupby(['profile']).apply(lambda x: x.to_json(orient='records')).to_dict()

No luck with anything I tried. I have 10000+ rows that needs to be formatted in json. Any help would be appreciated! thanks in advance

答案1

得分: 1

使用以下方法:

df.groupby('profile')['id'].apply(list)\
    .reset_index(name='ids').to_dict(orient='records')

[{'profile': 'SG-1234-BOM-A',
  'ids': ['8298-hdfbs-37483-s', 'dhduf-7435-4897h-a']},
 {'profile': 'SG-4567-PUN02', 'ids': ['game-uio-09349-831']},
 {'profile': 'SG-7527-DEL02',
  'ids': ['2342-5648-9637-abc',
   '4587-9721-4547-ytf',
   '4654-a123-b789-09d',
   '1234-gt3g-56qw-321',
   '9897-93729-fghb-df',
   'uidr-9087-tyip-012',
   'hexa-tata-1234-fd3']}]
英文:

Use the following approach:

df.groupby('profile')['id'].apply(list)\
    .reset_index(name='ids').to_dict(orient='records')

[{'profile': 'SG-1234-BOM-A',
  'ids': ['8298-hdfbs-37483-s', 'dhduf-7435-4897h-a']},
 {'profile': 'SG-4567-PUN02', 'ids': ['game-uio-09349-831']},
 {'profile': 'SG-7527-DEL02',
  'ids': ['2342-5648-9637-abc',
   '4587-9721-4547-ytf',
   '4654-a123-b789-09d',
   '1234-gt3g-56qw-321',
   '9897-93729-fghb-df',
   'uidr-9087-tyip-012',
   'hexa-tata-1234-fd3']}]

huangapple
  • 本文由 发表于 2023年3月3日 21:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定