简体   繁体   English

如何从同一个 dataframe 中的字典键创建列?

[英]How can I create column from dictionary keys in same dataframe?

I have a dataframe, something like:我有一个 dataframe,类似于:

|   | a | b                |
|---|---|------------------|
| 0 | a | {'d': 1, 'e': 2} |
| 1 | b | {'d': 3, 'e': 4} |
| 2 | c | NaN              |
| 3 | d | {'f': 5}         |

How can make something like this:怎么能做出这样的事情:

|   | a | b                | d | e | f |
|---|---|------------------|---|---|---|
| 0 | a | {'d': 1, 'e': 2} | 1 | 2 |nan|
| 1 | b | {'d': 3, 'e': 4} | 3 | 4 |nan|
| 2 | c | NaN              |nan|nan|nan|
| 3 | d | {'f': 5}         |nan|nan| 5 |


I tried doing this Split / Explode a column of dictionaries into separate columns with pandas but due to null values present, it is throwing an error.我尝试使用 pandas 将一列字典拆分/分解成单独的列,但由于存在 null 值,它会引发错误。
'float' object has no attribute 'items' “浮动” object 没有属性“项目”

You can try the following:您可以尝试以下方法:

>>> df
   a                 b
0  a  {'d': 1, 'e': 2}
1  b  {'d': 3, 'e': 4}
2  c               NaN
3  d          {'f': 5}

>>> df.join(pd.DataFrame.from_records(df['b'].mask(df.b.isna(), {}).tolist()))

   a                 b    d    e    f
0  a  {'d': 1, 'e': 2}  1.0  2.0  NaN
1  b  {'d': 3, 'e': 4}  3.0  4.0  NaN
2  c               NaN  NaN  NaN  NaN
3  d          {'f': 5}  NaN  NaN  5.0

Replace NaN with None and then proceed将 NaN 替换为 None 然后继续

df = pd.DataFrame({'a':['a','b','c','d'], 
                   'b':[{'d': 1, 'e': 2},
                        {'d': 3, 'e': 4},
                        np.nan,
                        {'f': 5}]
                   })

df = df.where(pd.notnull(df), None)
pd.concat([df, df['b'].apply(pd.Series)], axis=1)

Output: Output:

   a                 b    d    e    f
0  a  {'d': 1, 'e': 2}  1.0  2.0  NaN
1  b  {'d': 3, 'e': 4}  3.0  4.0  NaN
2  c              None  NaN  NaN  NaN
3  d          {'f': 5}  NaN  NaN  5.0

暂无
暂无

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

相关问题 如何从同一数据框中的字典键创建具有键名的列? - How can I create column having key name from dictionary keys in same dataframe? 如何使用字典中的键作为索引创建 pandas DataFrame? [蟒蛇,熊猫] - How can I create a pandas DataFrame with keys from a dictionary as my index? [Python, pandas] 从另一个具有字典键的列创建pandas dataframe列 - Create pandas dataframe column from another column that has dictionary keys 如何从 dataframe 创建字典,其中键是列名,值是每列下的值数? - how to create a dictionary from a dataframe where the keys are column names and values are the number of values under each column? 使用键作为新列从字典创建数据框? - create a dataframe from a dictionary using the keys as a new column? 从 dataframe 创建一个字典,第一列作为键,其余作为值 - Create a Dictionary from dataframe with first column as keys and remaining as values 如何在不指定熊猫的列名的情况下从数据框创建字典? - How can i create a dictionary from a dataframe without specifying column names in pandas? 如何从字典中创建一个pandas数据框,列名作为键,值作为行,其中值是二维数组 - how to create a pandas dataframe from a dictionary with column names as keys and values as rows where the values are 2-d array 如何从嵌套字典创建平面字典,其字符串是引用字典的子集? - How can I create a flat dictionary from a nested dictionary whose keys are a subset of a reference dictionary? 如何为列表中的一个键创建具有多个值的 Python 字典,然后创建具有一列和多行的 pandas 数据框 - How can I create a Python dictionary with multiple values for one key from a list, to then create a pandas dataframe with one column and multiple rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM