将Pandas单元格中的列表分成多列 [英] Splitting a list in a Pandas cell into multiple columns

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

问题描述

我有一个非常简单的Pandas dataframe,其中每个单元格都包含一个列表.我想将列表的每个元素拆分成自己的列.我可以通过导出值然后创建一个新的dataframe来做到这一点.尤其是如果我的dataframe除了列表列之外还有一列,这似乎不是一个好方法.

I have a really simple Pandas dataframe where each cell contains a list. I'd like to split each element of the list into it's own column. I can do that by exporting the values and then creating a new dataframe. This doesn't seem like a good way to do this especially, if my dataframe had a column aside from the list column.

import pandas as pd

df = pd.DataFrame(data=[[[8,10,12]],
                        [[7,9,11]]])

df = pd.DataFrame(data=[x[0] for x in df.values])

所需的输出:

   0   1   2
0  8  10  12
1  7   9  11

根据@Psidom回答进行跟进:

Follow-up based on @Psidom answer:

如果我确实有第二列:

df = pd.DataFrame(data=[[[8,10,12], 'A'],
                        [[7,9,11], 'B']])

如何不松开另一列?

所需的输出:

   0   1   2  3 
0  8  10  12  A
1  7   9  11  B

推荐答案

您可以使用apply()函数遍历Series,并将每个列表转换为Series,这会自动在列方向上将列表扩展为一系列:

You can loop through the Series with apply() function and convert each list to a Series, this automatically expand the list as a series in the column direction:

df[0].apply(pd.Series)

#   0    1   2
#0  8   10  12
#1  7    9  11

更新:要保留数据框的其他列,可以将结果与要保留的列连接起来:

Update: To keep other columns of the data frame, you can concatenate the result with the columns you want to keep:

pd.concat([df[0].apply(pd.Series), df[1]], axis = 1)

#   0    1   2  1
#0  8   10  12  A
#1  7    9  11  B

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

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