pandas -将列值合并到新列的列表中 [英] Pandas - combine column values into a list in a new column

查看:84
本文介绍了 pandas -将列值合并到新列的列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python Pandas数据框df:

I have a Python Pandas dataframe df:

d=[['hello',1,'GOOD','long.kw'],
   [1.2,'chipotle',np.nan,'bingo'],
   ['various',np.nan,3000,123.456]]                                                    
t=pd.DataFrame(data=d, columns=['A','B','C','D']) 

看起来像这样:

print(t)
         A         B     C        D
0    hello         1  GOOD  long.kw
1      1.2  chipotle   NaN    bingo
2  various       NaN  3000  123.456

我正在尝试创建一个新列,该列是ABCD中的值的list.所以看起来像这样:

I am trying to create a new column which is a list of the values in A, B, C, and D. So it would look like this:

t['combined']                                             

Out[125]: 
0        [hello, 1, GOOD, long.kw]
1        [1.2, chipotle, nan, bingo]
2        [various, nan, 3000, 123.456]
Name: combined, dtype: object

我正在尝试以下代码:

t['combined'] = t.apply(lambda x: list([x['A'],
                                        x['B'],
                                        x['C'],
                                        x['D']]),axis=1)    

哪个返回此错误:

ValueError: Wrong number of items passed 4, placement implies 1 

令我感到困惑的是,如果要删除要添加到列表中的一列(或将另一列添加到我不希望添加到列表中的数据框中),则我的代码可以正常工作.

What is puzzling to me is if remove one of the columns that I want to put in the list (or add another column to the dataframe that I DON'T add to the list), my code works.

例如,运行以下代码:

t['combined'] = t.apply(lambda x: list([x['A'],
                                        x['B'],
                                        x['D']]),axis=1)      

如果我只需要3列,则返回完美的结果

Returns this which is perfect if I only wanted the 3 columns:

print(t)
         A         B     C        D                 combined
0    hello         1  GOOD  long.kw      [hello, 1, long.kw]
1      1.2  chipotle   NaN    bingo   [1.2, chipotle, bingo]
2  various       NaN  3000  123.456  [various, nan, 123.456]

我完全不知道为什么要求对数据帧中的所有列进行合并"列表会产生错误,但是选择除1列以外的所有列来创建合并"列表,并且将列表创建为预期的.

I am at a complete loss as to why requesting the 'combined' list be made of all columns in the dataframe would create an error, but selecting all but 1 column to create the 'combined' list and the list is created as expected.

推荐答案

尝试一下:

t['combined']= t.values.tolist()

t
Out[50]: 
         A         B     C        D                       combined
0    hello         1  GOOD  long.kw      [hello, 1, GOOD, long.kw]
1     1.20  chipotle   NaN    bingo    [1.2, chipotle, nan, bingo]
2  various       NaN  3000   123.46  [various, nan, 3000, 123.456]

这篇关于 pandas -将列值合并到新列的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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