Reputation: 305
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!
Upvotes: 1
Views: 162
Reputation: 730
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.
Upvotes: 1