将dataframe列中的列表拆分为多个列 [英] Split lists within dataframe column into multiple columns

查看:2145
本文介绍了将dataframe列中的列表拆分为多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Pandas DataFrame列,其中一个列表中有多个列表.像这样:

I have a Pandas DataFrame column with multiple lists within a list. Something like this:

df
     col1
0    [[1,2], [2,3]]
1    [[a,b], [4,5], [x,y]] 
2    [[6,7]]

我想将列表分成多列,因此输出应类似于:

I want to split the list over multiple columns so the output should be something like:

    col1    col2     col3
0   [1,2]   [2,3]   
1   [a,b]   [4,5]    [x,y]
2   [6,7]

请帮助我.预先感谢

推荐答案

如果性能很重要,我认为需要DataFrame建设者:

I think need DataFrame contructor if performance is important:

df = pd.DataFrame(df['col1'].values.tolist())
print (df)
        0       1       2
0  [1, 2]  [2, 3]    None
1  [a, b]  [4, 5]  [x, y]
2  [6, 7]    None    None

如果需要删除NaN s-缺少值,请首先添加

If need remove NaNs - missing values first add dropna:

df = pd.DataFrame(df['col1'].dropna().values.tolist())

这篇关于将dataframe列中的列表拆分为多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆