如何将panda.series转换为数据框

时间:2019-08-26 23:03:22

标签: python pandas

如何转换下面提到的熊猫数据框

dateint    client_id      reactions
20190821   heryi-789      [{count=1, name=hp, users=[xyz1]}]
20190821   ywie-234       [{count=1, name=art, users=[abc2]}, 
                           {count=1, name=wck, users=[xyz]}]
20190821   uiwo-238       [{count=6, name=rtf, users=[tst23, ert34, 
                            abc2]}]

TO

dateint        client_id    count   name    users
20190821       heryi-789    1       hp      xyz1
20190821       ywie-234     1       art     abc2
20190821       ywie-234     1       wck     xyz
20190821       uiwo-238     3       rtf     tst23
20190821       uiwo-238     3       rtf     ert34
20190821       uiwo-238     3       rtf     abc2

我尝试将“反应”列值转换为dict,但是具有多个值的“用户”值未转换为列表

1 个答案:

答案 0 :(得分:0)

尝试一下

df['count'] = df['reactions'].apply(lambda x:x['count'])
df['name'] = df['reactions'].apply(lambda x:x['name'])
df['users'] = df['reactions'].apply(lambda x:x['users'])

如果熊猫版本大于.25,则使用df['users'].explode()