简体   繁体   English

如何将包含字典列表的 dataframe 列转换为单独的列?

[英]How to convert dataframe column which contains list of dictionary into separate columns?

I have a dataframe column which looks like this:我有一个 dataframe 列,如下所示:

df_cost['region.localCurrency']:

0     [{'content': 'Dirham', 'languageCode': 'EN'}]
1     [{'content': 'Dirham', 'languageCode': 'EN'}]
2     [{'content': 'Dirham', 'languageCode': 'EN'}]
3       [{'content': 'Euro', 'languageCode': 'DE'}]
4       [{'content': 'Euro', 'languageCode': 'DE'}]
5       [{'content': 'Euro', 'languageCode': 'DE'}]
6       [{'content': 'Euro', 'languageCode': 'DE'}]
7       [{'content': 'Euro', 'languageCode': 'DE'}]
8       [{'content': 'Euro', 'languageCode': 'DE'}]
9       [{'content': 'Euro', 'languageCode': 'DE'}]
10      [{'content': 'Euro', 'languageCode': 'DE'}]
11      [{'content': 'Euro', 'languageCode': 'DE'}]
12      [{'content': 'Euro', 'languageCode': 'DE'}]
13    [{'content': 'Dirham', 'languageCode': 'EN'}]
14    [{'content': 'Dirham', 'languageCode': 'EN'}]
15    [{'content': 'Dirham', 'languageCode': 'EN'}]
16      [{'content': 'Euro', 'languageCode': 'DE'}]
17      [{'content': 'Euro', 'languageCode': 'DE'}]
18      [{'content': 'Euro', 'languageCode': 'DE'}]
19      [{'content': 'Euro', 'languageCode': 'DE'}]
Name: region.localCurrency, dtype: object

and I want to convert it, to separate the dictionary keys and values into columns.我想转换它,将字典键和值分成列。 I want to add two separate columns to the initial df_cost dataframe, like 'localCurrencyContent' and 'localCurrencyCode', based on the dictionary contents of region.localCurrency.我想根据 region.localCurrency 的字典内容向初始 df_cost dataframe 添加两个单独的列,例如“localCurrencyContent”和“localCurrencyCode”。 I tried to split the region.localCurrency column like:我试图像这样拆分 region.localCurrency 列:

df_split=pd.DataFrame(df_cost['region.localCurrency'].apply(pd.Series), columns=['localCurrencyContent', 'localCurrencyCode'])
print(df_split)

but this gives me NaN values for the localCurrencyContent and localCurrencyCode, instead of 'Euro' and 'DE' for example.但这给了我 localCurrencyContent 和 localCurrencyCode 的 NaN 值,而不是例如“Euro”和“DE”。 How could I split the column "region.localCurrency" and add the two created columns to the cost_df, initial dataframe?我如何拆分列“region.localCurrency”并将创建的两个列添加到 cost_df,初始 dataframe?

Pandas.json_normalize will probably do the job for you. Pandas.json_normalize 可能会为您完成这项工作。 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.json_normalize.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.json_normalize.html

Use json_normalize with convert first values by indexing:使用json_normalize通过索引转换第一个值:

d = {'content':'localCurrencyContent','languageCode':'localCurrencyCode'}
df1 = pd.json_normalize(df_cost.pop('region.localCurrency').str[0]).rename(columns=d)
df = df_cost.join(df1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字典有一个单独的字典,我想在 python 中的 dataframe 中转换它,以便该表包含具有子列的列 - A dictionary has a separate dictionary and i want to convert it in dataframe in python such that the table contains columns which has sub columns 如何将字典格式的数据框中的列转换为单独的列? - How do a convert a column in a dataframe that is in the format of a dictionary into separate columns? 如何将带有字典的 dataframe 列转换为行和列? - How to convert dataframe column with dictionary to rows and columns? 将字典列拆分为单独的列,融化数据框 - Split list of dictionary column to separate columns, melting the dataframe 如何将字典转换为 pandas dataframe,键和值位于两个单独的列中? - How to convert a dictionary into a pandas dataframe with key and values in two separate columns? 用字典交叉 dataframe 列“包含列表” - Cross dataframe columns "contains List" with dictionary 将带有字典字典的 pandas 列转换为单独的列 - convert pandas column with dictionary of dictionaries to separate columns 如何将包含np数组的2列连接到数据框中的新列 - How to concatenate 2 columns,which contains np arrays, into a new column in dataframe 如何在数据框中按列分组,该列包含一个包含元组列表的列 - How to groupby a column in dataframe which contains a column containing list of tuples 如何从列表的 Dataframe 的两列中创建字典? - how to create dictionary, from two columns of Dataframe which is list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM