如何将panda.series转换为datafram

2024-05-17 19:44:33 发布

您现在位置:Python中文网/ 问答频道 /正文

如何转换下面提到的数据帧

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]}]

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

我尝试将“reactions”列值转换为dict,但是具有多个值的“users”值不会转换为list


Tags: nameclientiddateintcountusershprtf
1条回答
网友
1楼 · 发布于 2024-05-17 19:44:33

试试这个

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()

相关问题 更多 >