简体   繁体   English

使用数据框列中的键对值创建新列

[英]Create new column using keys pair value from a dataframe column

I have a data frame with many column.我有一个包含许多列的数据框。 One of the column is named 'attributes' and in it has a list of dictionary with keys and values.其中一列名为“属性”,其中包含一个包含键和值的字典列表。 I want to extract each keys and it values to it own column.我想提取每个键并将其值提取到它自己的列中。 This is what the data frame look like这是数据框的样子

The following will add the dictionary keys as additional columns, keeping the attributes column in the dataframe:以下将字典键添加为附加列,将attributes列保留在数据框中:

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

Edit编辑

For the nested dictionaries, trying this simple example worked for me (here the initial column of dictionaries is colC , with the nested dictionaries in foo ):对于嵌套字典,尝试这个简单的示例对我有用(这里字典的初始列是colC ,嵌套字典在foo中):

import pandas as pd

df = pd.DataFrame(
    {
        'colA': {0: 7, 1: 2, 2: 5, 3: 3, 4: 5},
        'colB': {0: 7, 1: 8, 2: 10, 3: 2, 4: 5},
        'colC': {
            0: {'foo': {"A": 5, "B": 6, "C": 9}, 'bar': 182, 'baz': 148},
            1: {'bar': 103, 'baz': 155},
            2: {'foo': 165, 'bar': 184, 'baz': 170},
            3: {'foo': 121, 'bar': 151, 'baz': 187},
            4: {'foo': 137, 'bar': 199, 'baz': 108},
        },
    }
)

df = pd.concat([df, df["colC"].apply(pd.Series)], axis=1)
#   colA  colB                                                       colC                       foo    bar    baz
#0     7     7  {'foo': {'A': 5, 'B': 6, 'C': 9}, 'bar': 182, 'baz': 148}  {'A': 5, 'B': 6, 'C': 9}  182.0  148.0
#1     2     8                                   {'bar': 103, 'baz': 155}                       NaN  103.0  155.0
#2     5    10                       {'foo': 165, 'bar': 184, 'baz': 170}                       165  184.0  170.0
#3     3     2                       {'foo': 121, 'bar': 151, 'baz': 187}                       121  151.0  187.0
#4     5     5                       {'foo': 137, 'bar': 199, 'baz': 108}                       137  199.0  108.0

df = pd.concat([df, df["foo"].apply(pd.Series)], axis=1)
#   colA  colB                                                       colC                       foo    bar    baz      0    A    B    C
#0     7     7  {'foo': {'A': 5, 'B': 6, 'C': 9}, 'bar': 182, 'baz': 148}  {'A': 5, 'B': 6, 'C': 9}  182.0  148.0    NaN  5.0  6.0  9.0
#1     2     8                                   {'bar': 103, 'baz': 155}                       NaN  103.0  155.0    NaN  NaN  NaN  NaN
#2     5    10                       {'foo': 165, 'bar': 184, 'baz': 170}                       165  184.0  170.0  165.0  NaN  NaN  NaN
#3     3     2                       {'foo': 121, 'bar': 151, 'baz': 187}                       121  151.0  187.0  121.0  NaN  NaN  NaN
#4     5     5                       {'foo': 137, 'bar': 199, 'baz': 108}                       137  199.0  108.0  137.0  NaN  NaN  NaN

There is the column 0 which appears because of the "empty" rows, but this should not be a problem.由于“空”行,出现了第0列,但这应该不是问题。

暂无
暂无

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

相关问题 使用键作为新列从字典创建数据框? - create a dataframe from a dictionary using the keys as a new column? 使用column及其值在pandas数据框中创建一个新列 - Create a new column in pandas dataframe using column and its value 创建新的数据框列,保留另一列的第一个值 - Create new dataframe column keeping the first value from another column 从 dataframe 中提取列并使用正则表达式创建新列 - Extract column from dataframe and create new column using regex 根据列值的条件(不使用.ix)从主 dataframe 创建新的 dataframe - Create new dataframe from main dataframe based on condition of column value (without using .ix) 使用来自另一个数据帧的 if 条件在 Pandas 数据帧中创建一个新列 - create a new column in pandas dataframe using if condition from another dataframe 从 dataframe 列中查找不同的值并在数据帧中创建新列并在新列中设置频率 - Find distinct value from dataframe column and create new column in datafrase and set frequency in new column 使用字典中的键在 pandas dataframe 中查找相同的“键”,然后将字典中的值分配给 dataframe 空列 - Using keys from a dict to look for the same "keys" in a pandas dataframe, then assign value from dict to dataframe empty column 如何使用 Pyspark DataFrame 创建具有空值的新列? - How to create a new column with a null value using Pyspark DataFrame? 我想使用数据框中的 dict 值在数据框中创建一列 - I want to create a column in a dataframe using a dict value from the dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM