请考虑以下简化的Pandas数据框架:
名称 | 组件 | D800465 | [{'component': 'comp1', 'version': '1.0.0'}, {'component': 'comp2', 'version': '15.2.5'}] | [{'component': 'comp1', 'version': '1.0.0'}, {'component': 'comp2', 'version': '15.2.5'}, {'component': 'comp3', 'version': '2.5'}] |
---|---|
L908041 | [{'component': 'comp1', 'version': '1.0.0'}] |
D797502 | [{'component': 'comp1', 'version': '1.0.0'}] |
您可以使用explode
和pandas.json_normalize
:
df2 = df.explode('Component')
df2 = (df2[['Name']].reset_index(drop=True)
.join(pd.json_normalize(df2['Component']))
)
输出:
Name component version
0 D800465 comp1 1.0.0
1 D800465 comp2 15.2.5
2 L932227 comp1 1.0.0
3 L932227 comp2 15.2.5
4 L932227 comp3 2.5
5 L908041 comp1 1.0.0
6 D797502 comp1 1.0.0