本文介绍了将包含字典列表的列转换为pandas数据框中的多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Pandas数据框,如:
I have a Pandas dataframe like :
pd.DataFrame({'a':[1,2], 'b':[[{'c':1,'d':5},{'c':3, 'd':7}],[{'c':10,'d':50}]]})
Out[2]:
a b
0 1 [{u'c': 1, u'd': 5}, {u'c': 3, u'd': 7}]
1 2 [{u'c': 10, u'd': 50}]
如果要在"b"中包含多个元素,我想扩展"b"列并重复"a"列,如下所示:
And I want to expand the 'b' column and repeat 'a' column if there are more than one element in 'b' as follow:
Out[2]:
a c d
0 1 1 5
1 1 3 7
2 2 10 50
我尝试在每一行上使用apply函数,但是我没有成功,显然是apply将一行转换为一行.
I tried to use apply function on each row but I was not successful, apparently apply convert one row to one row.
推荐答案
您可以使用 concat
与list comprehension
:
df = pd.concat([pd.DataFrame(x) for x in df['b']], keys=df['a'])
.reset_index(level=1, drop=True).reset_index()
print (df)
a c d
0 1 1 5
1 1 3 7
2 2 10 50
如果索引是唯一的,则可以使用 join
用于所有列:
If index is unique, then is possible use join
for all columns:
df1 = pd.concat([pd.DataFrame(x) for x in df['b']], keys=df.index)
.reset_index(level=1,drop=True)
df = df.drop('b', axis=1).join(df1).reset_index(drop=True)
print (df)
a c d
0 1 1 5
1 1 3 7
2 2 10 50
我尝试简化解决方案:
l = df['b'].str.len()
df1 = pd.DataFrame(np.concatenate(df['b']).tolist(), index=np.repeat(df.index, l))
df = df.drop('b', axis=1).join(df1).reset_index(drop=True)
print (df)
a c d
0 1 1 5
1 1 3 7
2 2 10 50
这篇关于将包含字典列表的列转换为pandas数据框中的多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!