熊猫爆炸成多列(不同的嵌套列表长度)

时间:2020-10-12 16:35:37

标签: python pandas numpy

为了使用具有不同长度的多个属性,我一直在努力挣扎。

这是示例数据-json响应

main = {
    'clothes': [{
            'id': 13,
            'color': 'red',
            'contributor_info': [],
            'commentor_info': []
        },
        {
            'id': 15,
            'color': 'yellow',
            'contributor_info': [{
                    'id': 35,
                    'name': 'Anthony',
                    'categories': ['Social'],
                    'first_seen_date': '2020-07-29',
                    'last_seen_date': '2020-07-29'
                },
                {
                    'id': 67,
                    'name': 'Betsy',
                    'categories': ['Networking'],
                    'first_seen_date': '2020-08-31',
                    'last_seen_date': '2020-09-29'
                }
            ],
            'commentor_info': [{
                'id': 3822,
                'name': 'Future',
                'categories': ['Work'],
                'first_seen_date': '2020-09-29',
                'last_seen_date': '2020-09-29'
            }]
        },
        {
            'id': 17,
            'color': 'blue',
            'contributor_info': [{
                    'id': 468,
                    'name': 'Cassendra',
                    'categories': ['Social'],
                    'first_seen_date': '2020-07-29',
                    'last_seen_date': '2020-07-29'
                },
                {
                    'id': 690,
                    'name': 'Dan',
                    'categories': ['Networking'],
                    'first_seen_date': '2020-08-31',
                    'last_seen_date': '2020-09-29'
                }
            ],
            'commentor_info': [{
                    'id': 34,
                    'name': 'Fischer',
                    'categories': ['Work'],
                    'first_seen_date': '2020-09-29',
                    'last_seen_date': '2020-09-29'
                },
                {
                    'id': 985,
                    'name': 'Candice',
                    'categories': ['Work'],
                    'first_seen_date': '2020-10-02',
                    'last_seen_date': '2020-10-02'
                }
            ]
        }
    ]
}   

我想弄平所需的格式(仅包含颜色以及contributor_info和commentor_info中的所有属性)---

color  contri/comment         name    categories   first         last   
red       n/a                 n/a       n/a         n/a           n/a       
yellow  contributor_info    Anthony    Social     '2020-07-29'   '2020-07-29' 
yellow  contributor_info    Betsy      Networking '2020-08-31'   '2020-09-29' 
yellow  commentor_info      Future     Work       '2020-09-29'   '2020-09-29'
blue    contributor_info    Cassendra  Social     '2020-07-29'   '2020-07-29'
blue    contributor_info    Dan        Networking '2020-08-31'   '2020-09-29'
blue    commentor_info      Fischer    Work       '2020-09-29'   '2020-09-29'
blue    commentor_info      Candice    Work       '2020-10-02'   '2020-10-02'

放入数据框后 df =pd.DataFrame.from_dict(main['clothes']) 我已经尝试过.apply(pd.Series).stack()。reset_index() 并爆炸功能,但并没有使嵌套的属性扁平化

真的很感谢您的帮助/建议。预先感谢!

0 个答案:

没有答案