[英]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.