如何将 Pandas 中的一列扩展为多列? [英] How to expand one column in Pandas to many columns?
本文介绍了如何将 Pandas 中的一列扩展为多列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
作为标题,我在pandas中有一个列(系列),它的每一行都是一个类似于[0,1,2,3,4,5]
的列表.每个列表有 6 个数字.我想把这一列改成6列,比如[0,1,2,3,4,5]
会变成6列,其中0
就是第一列,1
是第二列,2
是第三列,依此类推.我该怎么做?
As the title, I have one column (series) in pandas, and each row of it is a list like [0,1,2,3,4,5]
. Each list has 6 numbers. I want to change this column into 6 columns, for example, the [0,1,2,3,4,5]
will become 6 columns, with 0
is the first column, 1
is the second, 2
is the third and so on. How can I make it?
推荐答案
不如@jezrael 的解决方案快.但优雅:-)
Not as fast as @jezrael's solution. But elegant :-)
apply
和 pd.Series
df.a.apply(pd.Series)
0 1 2 3 4 5
0 0 1 2 3 4 5
1 0 1 2 3 4 5
或
df.a.apply(pd.Series, index=list('abcdef'))
a b c d e f
0 0 1 2 3 4 5
1 0 1 2 3 4 5
这篇关于如何将 Pandas 中的一列扩展为多列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文