Python 如何转换每个嵌套字典';元素添加到新列?

Python 如何转换每个嵌套字典';元素添加到新列?,python,pandas,dataframe,dictionary,nested,Python,Pandas,Dataframe,Dictionary,Nested,我有以下数据帧结构。共有两(2)列:id和info(对象) 我想将此格式转换为以下格式: id route_id stop_id 0 14050000893760073 1 1 1 14050000893760073 2 2 有什么想法吗?提前谢谢你 df2 = df.explode('info', ignore_index=True) df2 id i

我有以下数据帧结构。共有两(2)列:
id
info
(对象)

我想将此格式转换为以下格式:

                  id  route_id  stop_id
0  14050000893760073         1        1
1  14050000893760073         2        2
有什么想法吗?提前谢谢你

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

首先,分解
info
列中的列表。然后,从该列创建一个数据系列。最后,将
info\u df
和数据帧连接起来,以得到最终结果。

df[['id']].join(pd.dataframe(df['info'].explode().tolist())
尝试一下,因为我无法复制您的数据帧question@anky我得到了这个错误:TypeError:type'float'的对象没有len()给我们
print(df.head())
因为我刚刚guessed@ankyprint(df.head())与我最初发布的内容相同
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