Python Pandas - 将列值组合成新列中的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43898035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas - combine column values into a list in a new column
提问by clg4
I have a Python Pandas dataframe df:
我有一个 Python Pandas 数据框 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'])
which looks like this:
看起来像这样:
print(t)
A B C D
0 hello 1 GOOD long.kw
1 1.2 chipotle NaN bingo
2 various NaN 3000 123.456
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:
我想创建一个新的列是一个list
中值的A
,B
,C
,和D
。所以它看起来像这样:
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
I am trying this code:
我正在尝试这个代码:
t['combined'] = t.apply(lambda x: list([x['A'],
x['B'],
x['C'],
x['D']]),axis=1)
Which returns this error:
返回此错误:
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.
令我困惑的是,如果删除我想放入列表中的一列(或将另一列添加到我不添加到列表中的数据框),我的代码可以工作。
For instance, run this code:
例如,运行以下代码:
t['combined'] = t.apply(lambda x: list([x['A'],
x['B'],
x['D']]),axis=1)
Returns this which is perfect if I only wanted the 3 columns:
如果我只想要 3 列,则返回这是完美的:
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]
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.
我完全不知道为什么要求由数据帧中的所有列组成“组合”列表会产生错误,但是选择除 1 列之外的所有列来创建“组合”列表,并且按预期创建列表。
回答by Steven G
try this :
尝试这个 :
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]