简体   繁体   English

将字典列中的键和值转换为不同的列 Pandas

[英]Convert Key and Value in Dictionary Column as Different Columns Pandas

I have a table like this我有一张这样的桌子

Name    Type Food   Variant and Price
A       Cake        {‘Choco’:100, ‘Cheese’:100, ‘Mix’: 125}
B       Drinks      {‘Cola’:25, ‘Milk’:35}
C       Side dish   {‘French Fries’:20}
D       Bread       {None:10}

I want to use the keys and values of dictionaries in the Variant and Price column as 2 different columns but I am still confused, here is the output that I want:我想将 Variant 和 Price 列中字典的键和值用作 2 个不同的列,但我仍然感到困惑,这是我想要的 output:

Name    Type Food   Variant          Price
A       Cake        Choco            100
A       Cake        Cheese           100
A       Cake        Mix              125
B       Drinks      Cola             25
B       Drinks      Milk             35
C       Side dish   French Fries     20
D       Bread       NaN              10

Can anyone help me to figure it out?谁能帮我弄清楚?

Create list of tuples and then use DataFrame.explode , last create 2 columns:创建元组列表,然后使用DataFrame.explode ,最后创建 2 列:

df['Variant and Price'] = df['Variant and Price'].apply(lambda x: list(x.items()))
df = df.explode('Variant and Price').reset_index(drop=True)
df[['Variant','Price']] = df.pop('Variant and Price').to_numpy().tolist()
print (df)
  Name  Type Food       Variant  Price
0    A       Cake         Choco    100
1    A       Cake        Cheese    100
2    A       Cake           Mix    125
3    B     Drinks          Cola     25
4    B     Drinks          Milk     35
5    C  Side dish  French Fries     20
6    D      Bread          None     10

Or create 2 columns and then use DataFrame.explode :或创建 2 列,然后使用DataFrame.explode

df['Variant'] = df['Variant and Price'].apply(lambda x: list(x.keys()))
df['Price'] = df.pop('Variant and Price').apply(lambda x: list(x.values()))
df = df.explode(['Variant', 'Price']).reset_index(drop=True)

暂无
暂无

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

相关问题 使用基于2个键列和1个值列的嵌套字典将pandas数据帧转换为字典 - Convert pandas dataframe to dictionary with nested dictionary based on 2 key columns and 1 value column Pandas - 将带有字典的列拆分为带有键和值的两列 - Pandas - split column with dictionary into two columns with key and value Pandas 按列分组并将其他列转换为键:值对 - Pandas group by a column and convert other columns to key:value pair 将熊猫列中的字典转换为字典值 - Convert dictionary in pandas' column to dictionary value 如何将具有3列的csv转换为以1列为键,1列为其值的字典并获取键的值 - How can I convert a csv with 3 columns to a dictionary with 1 column as key, 1 column as its value and fetch the value for key 如何将两个熊猫列转换为字典,但将同一第一列(键)的所有值合并为一个键? - How to convert two pandas columns into a dictionary, but merge all values of same first column (key) into one key? Pandas - 将两列作为字典转换为新列 - Pandas - Convert two columns into a new column as a dictionary 将带有字典字典的 pandas 列转换为单独的列 - convert pandas column with dictionary of dictionaries to separate columns Pandas:将字典列转换为键和值列 - Pandas: Dictionary column into key and value column 如何将多键字典转换为 pandas dataframe,其中每个键和值都有自己的列? - How to convert a multi-key dictionary to a pandas dataframe, where each key and value has its own column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM