如何使用python将数据框中的列表或字典转换为数据框中的多列?

问题描述 投票:0回答:0

我现在在数据框中有一个列名(custom_fields),我想将此列拆分为多个列我尝试了不同的方式来实现这个目标但问题没有解决。我试过这段代码但没有达到我的目标。下面是我试过的代码,

df['custom_fields'].apply(pd.Series)

df = pd.concat([df, df["custom_fields"].apply(pd.Series)], axis=1)

df = pd.concat([df, df["custom_fields"].apply(lambda x: pd.Series(x, dtype= "object"))], axis=1)

df.rename(columns={'id': 'custom_field_id', 'title': 'custom_field_title','description': 'custom_field_desc','type': 'custom_field_type','value': 'custom_field_value'}, inplace=True)

自定义字段 1 [{'id': 925826226, 'title': '风险评级', 'description': 'Maha 测试', 'type': 'Text', 'value': None}, {'id': 925839795, 'title': 'ABC 123', 'description': 'abc', 'type': 'Text', 'value': None}, {'id': 957251369, 'title': '风险等级 2', '描述':'风险等级 2','类型':'文本','值':无}]

python-3.x list spyder
© www.soinside.com 2019 - 2024. All rights reserved.