Skip to content
Advertisement

Pandas apply() with axis=0 unexpected behaviour

I’m using the .apply() method in pandas. I get the same results when using axis=0 and axis=1.

When using axis=0 I’d expect a series with four elements (indexed A, B, C, D) as a result. Can anyone tell me why the axis argument doesn’t work in this case?

I’m adding a reproducible example below. NOTE: I know that this is just a simple multiplication, which could be done simply using the method .mul() or the * operator, but I’m using here to keep things simple.

from pandas import util
df = util.testing.makeDataFrame()
df.head()
A B C D
Z82iXpl5s4 0.6031287648192426 -0.8271966886193748 1.8008948949143067 2.3722926060057237
MIFRqMYUeQ 0.2607658354228718 -0.2475715926867064 0.9548749309163952 2.682513736796777
F5qTLNGXAL -1.0716656707592347 -0.637969249754201 -0.6155681669722803 1.24399654282517
5J4v4avVEB -0.9685885791255543 -0.6735886163824997 -0.8065978319692303 0.5227282727367413
uEeRVYkeWU -0.6661417878211171 -0.15444679388555943 -0.06476868167309932 -0.8541859423835071
df.apply(lambda x: x * 9, axis=0).head()
A B C D
Z82iXpl5s4 5.428158883373183 -7.444770197574373 16.20805405422876 21.35063345405151
MIFRqMYUeQ 2.3468925188058463 -2.2281443341803575 8.593874378247557 24.142623631170995
F5qTLNGXAL -9.644991036833112 -5.741723247787809 -5.540113502750523 11.195968885426531
5J4v4avVEB -8.717297212129989 -6.062297547442497 -7.259380487723073 4.704554454630672
uEeRVYkeWU -5.995276090390054 -1.3900211449700348 -0.5829181350578939 -7.687673481451564
df.apply(lambda x: x * 9, axis=1).head()
A B C D
Z82iXpl5s4 5.428158883373183 -7.444770197574373 16.20805405422876 21.35063345405151
MIFRqMYUeQ 2.3468925188058463 -2.2281443341803575 8.593874378247557 24.142623631170995
F5qTLNGXAL -9.644991036833112 -5.741723247787809 -5.540113502750523 11.195968885426531
5J4v4avVEB -8.717297212129989 -6.062297547442497 -7.259380487723073 4.704554454630672
uEeRVYkeWU -5.995276090390054 -1.3900211449700348 -0.5829181350578939 -7.687673481451564

Advertisement

Answer

Looking at the documentation for pandas.DataFrame.apply:

axis : {0 or ‘index’, 1 or ‘columns’}, default 0
Axis along which the function is applied:

0 or ‘index’: apply function to each column.

1 or ‘columns’: apply function to each row.

Returns : Series or DataFrame Result of applying func along the given axis of the DataFrame.

When you are doing df.apply(lambda x: x * 9, axis=0), value in each column gets multiplied by 9, so there will be 4 series for each of the columns, and they are assigned back to the respective indices.

For more clarification, you can try something like this:

df.apply(lambda x: [(x.round(0)*9).tolist()], axis=0).T

                                                   0
A  [18.0, 18.0, 0.0, -9.0, -9.0, -9.0, -0.0, -0.0...
B  [-9.0, -9.0, -9.0, -9.0, -0.0, -0.0, 9.0, -18....
C  [9.0, -9.0, 18.0, -0.0, 0.0, -9.0, -0.0, 0.0, ...
D  [9.0, 18.0, -9.0, 9.0, -9.0, 9.0, 9.0, -9.0, 0...

Now, you’ll see those individual values.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement