Pandas按多列分组,得到一个多嵌套的Json。

问题描述 投票:0回答:1

我有一个数据框,看起来如下。

Lvl1  lvl2  lvl3  lvl4  lvl5
x     1x    3xx   1     "text1"
x     1x    3xx   2     "text2"
x     1x    3xx   3     "text3"
x     1x    4xx   4     "text4"
x     2x    4xx   5     "text5"
x     2x    4xx   6     "text6"
y     2x    5xx   7     "text7"
y     3x    5xx   8     "text8"
y     3x    5xx   9     "text9"
y     3x    6xx   10    "text10"
y     4x    7xx   11    "text11"
y     4x    7xx   62    "text12"
y     4x    8xx   62    "text13"
z
z
z
w
w
w

I would like to convert to nested json so it looks like this:

[{
  "x":{
         "1x":[{
                "3xx": [
                {
                lvl4: 1
                lvl5: "text1"
                },
                {
                lvl4: 2
                lvl5: "text2"
                },
                {
                lvl4: 3
                lvl5: "text3"
                }],
                "4xx": [
                {
                lvl4: 4
                lvl5: "text4"
                }],
         "2x":[{
                "4xx": [
                {
                lvl4: 5
                lvl5: "text5"
                },
                {
                lvl4: 6
                lvl5: "text6"
                }],
                "5xx": [
                {
                lvl4: 7
                lvl5: "text7"   
                }],
                }]

...

我使用的例子是 此处 作为开始,但我需要 lvl1, lvl2, lvl3 缩进如图所示的数据。 参考例子中返回的是同一级别的lvl1,lvl2,lvl3。

另外,我需要lvl的键是lvl的值。 例如 "x "而不是 "lvl1"。

[{
  "x":{

谢谢你的建议

python pandas pandas-groupby
1个回答
2
投票

根据预期的输出,你可以用三个嵌套的 groupby 的使用 to_dict. 有可能有更好的方法,但至少是一个开始。

[df.groupby('Lvl1')\
  .apply(lambda x: x.groupby('lvl2')\
                    .apply(lambda x: [x.groupby('lvl3')
                                       .apply(lambda x: x[['lvl4','lvl5']].to_dict('r')
                                              ).to_dict()]
                          ).to_dict()
  ).to_dict()]

[{'x': {'1x': [{'3xx': [{'lvl4': 1, 'lvl5': '"text1"'},
                        {'lvl4': 2, 'lvl5': '"text2"'},
                        {'lvl4': 3, 'lvl5': '"text3"'}],
                '4xx': [{'lvl4': 4, 'lvl5': '"text4"'}]
                }],
        '2x': [{'4xx': [{'lvl4': 5, 'lvl5': '"text5"'},
                        {'lvl4': 6, 'lvl5': '"text6"'}]}]},...

我只是对具体的外部格式有疑问

EDIT感谢@Trenton McKinney,看来如果你这样做。

df['lvl5'] = df['lvl5'].str.strip('"')
test = [df.groupby('Lvl1')\
          .apply(lambda x: x.groupby('lvl2')\
                            .apply(lambda x: [x.groupby('lvl3')
                                               .apply(lambda x: x[['lvl4','lvl5']].to_dict('r')
                                                      ).to_dict()]
                                  ).to_dict()
          ).to_dict()]

import json
json_res = list(map(json.dumps, test))

那么 json_res 可以满足json的需求

注意事项

  • 以下代码,将正确保存 test 为双引号的json格式
with open('data.json', 'w') as f:
    json.dump(test, f)
© www.soinside.com 2019 - 2024. All rights reserved.