简体   繁体   中英

How to convert each nested dictionary' element to a new pandas column?

I have the following pandas dataframe structure. There are two (2) columns: id and info (object)

                    id                                                                    info
0    14050000893760073  [{'route_id': '1', 'stop_id': '1'}, {'route_id': '2', 'stop_id': '2'}]

I would like to convert this format to the following format:

                  id  route_id  stop_id
0  14050000893760073         1        1
1  14050000893760073         2        2

Any ideas? Thank you in advance!

df2 = df.explode('info', ignore_index=True)
df2
   id                 info
0  14050000893760073  {'route_id': '1', 'stop_id': '1'}
1  14050000893760073  {'route_id': '2', 'stop_id': '2'}


info_df = df2["info"].apply(pd.Series)
info_df
     route_id  stop_id
0        1       1
1        2       2

result = pd.concat([df2, info_df], axis=1).drop('info', axis=1)
result
    id              route_id    stop_id
0   14050000893760073   1   1
1   14050000893760073   2   2

First, you explode the list that you have in the info column. Then, you create a data series out of that column. And at last, you concatenate the info_df and your dataframe to give the final result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM