从 pandas 专栏解开字典 [英] Unpack dictionary from Pandas Column
本文介绍了从 pandas 专栏解开字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,其中的一列作为字典.我想将其解压缩为多列(即代码,金额为以下Raw列格式的单独列).以下代码曾经与pandas v0.22(现在是(0.23))一起工作,给出了索引错误:
I have a dataframe that has one of the columns as a dictionary. I want to unpack it into multiple columns (i.e. code, amount are separate columns in the below Raw column format). The following code used to work with pandas v0.22, now (0.23) giving an index error:
pd.DataFrame.from_records(df.col_name.fillna(pd.Series([{'code':'not applicable'}], index=df.index)).values.tolist())
ValueError: Length of passed values is 1, index implies x
我搜索了google/stack溢出了几个小时,以前提供的其他解决方案都无法正常工作了.
I searched google/stack overflow for hours and none of the other solutions previously presented work anymore.
原始列格式:
dict_codes
0 {'code': 'xx', 'amount': '10.00',...
1 {'code': 'yy', 'amount': '20.00'...
2 {'code': 'bb', 'amount': '30.00'...
3 {'code': 'aa', 'amount': '40.00'...
10 {'code': 'zz', 'amount': '50.00'...
11 NaN
12 NaN
13 NaN
有人有什么建议吗?
谢谢
推荐答案
设置
df = pd.DataFrame(dict(
codes=[
{'amount': 12, 'code': 'a'},
{'amount': 19, 'code': 'x'},
{'amount': 37, 'code': 'm'},
np.nan,
np.nan,
np.nan,
]
))
df
codes
0 {'amount': 12, 'code': 'a'}
1 {'amount': 19, 'code': 'x'}
2 {'amount': 37, 'code': 'm'}
3 NaN
4 NaN
5 NaN
apply
与pd.Series
请务必先dropna
apply
with pd.Series
Make sure to dropna
first
df.codes.dropna().apply(pd.Series)
amount code
0 12 a
1 19 x
2 37 m
df.drop('codes', 1).assign(**df.codes.dropna().apply(pd.Series))
amount code
0 12.0 a
1 19.0 x
2 37.0 m
3 NaN NaN
4 NaN NaN
5 NaN NaN
tolist
和from_records
相同的想法,但跳过apply
tolist
and from_records
Same idea but skip the apply
pd.DataFrame.from_records(df.codes.dropna().tolist())
amount code
0 12 a
1 19 x
2 37 m
df.drop('codes', 1).assign(**pd.DataFrame.from_records(df.codes.dropna().tolist()))
amount code
0 12.0 a
1 19.0 x
2 37.0 m
3 NaN NaN
4 NaN NaN
5 NaN NaN
这篇关于从 pandas 专栏解开字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文