Why does df.apply (tuple) work, but not df.apply (list)?

Here: dataframe:

    A  B  C
0   6  2 -5
1   2  5  2
2  10  3  1
3  -5  2  8
4   3  6  2

I could get a column that is basically a tuple of columns from the original dfwith 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

But if I need a list of values ​​instead of a tuple of them, I cannot do this because it does not give me what I expect:

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  8
4   3  6  2

Instead, I need to do:

out = pd.Series(df.values.tolist())
print(out)

0    [6, 2, -5]
1     [2, 5, 2]
2    [10, 3, 1]
3    [-5, 2, 8]
4     [3, 6, 2]
dtype: object

Why can't I use df.apply(list, 1)to get what I want?


application

Timing of some possible workarounds:

df_test = pd.concat([df] * 10000, 0)

%timeit pd.Series(df.values.tolist()) # original workaround
10000 loops, best of 3: 161 µs per loop

%timeit df.apply(tuple, 1).apply(list, 1) # proposed by Alexander
1000 loops, best of 3: 615 µs per loop
+6
source share
1 answer

. func=tuple , func=list lib.reduce:

ValueError: ('function does not reduce', 0)

, , .

except, , pandas. , wont-fix dupe.

16321: create(),

15628: Dataframe.apply = True

, , , , .

, piRSquared ( pandas ), :

pd.Series([list(x) for x in df.itertuples(index=False)])

apply numpy ufunc .

+3

All Articles