相关疑难解决方法(0)

为什么df.apply(元组)工作但不是df.apply(list)?

这是一个数据帧:

    A  B  C
0   6  2 -5
1   2  5  2
2  10  3  1
3  -5  2  8
4   3  6  2
Run Code Online (Sandbox Code Playgroud)

我可以检索一个列,它基本上是原始列的元组df使用df.apply:

out = df.apply(tuple, 1)
print(out)

0    (6, 2, -5)
1     (2, 5, 2)
2    (10, 3, 1)
3    (-5, 2, 8)
4     (3, 6, 2)
dtype: object
Run Code Online (Sandbox Code Playgroud)

但是,如果我想要一个值列表而不是它们的元组,我不能这样做,因为它没有给我我期望的东西:

out = df.apply(list, 1)
print(out)

    A  B  C
0   6  2 -5
1   2  5  2
2  10  3  1
3  -5  2 …
Run Code Online (Sandbox Code Playgroud)

python list apply dataframe pandas

8
推荐指数
1
解决办法
517
查看次数

标签 统计

apply ×1

dataframe ×1

list ×1

pandas ×1

python ×1