Here is my code to generate a dataframe:
JavaScript
x
5
1
import pandas as pd
2
import numpy as np
3
4
dff = pd.DataFrame(np.random.randn(1,2),columns=list('AB'))
5
then I got the dataframe:
JavaScript
1
6
1
+------------+---------+--------+
2
| | A | B |
3
+------------+---------+---------
4
| 0 | 0.626386| 1.52325|
5
+------------+---------+--------+
6
When I type the commmand :
JavaScript
1
2
1
dff.mean(axis=1)
2
I got :
JavaScript
1
3
1
0 1.074821
2
dtype: float64
3
According to the reference of pandas, axis=1 stands for columns and I expect the result of the command to be
JavaScript
1
4
1
A 0.626386
2
B 1.523255
3
dtype: float64
4
So here is my question: what does axis in pandas mean?
Advertisement
Answer
It specifies the axis along which the means are computed. By default axis=0
. This is consistent with the numpy.mean
usage when axis
is specified explicitly (in numpy.mean
, axis==None by default, which computes the mean value over the flattened array) , in which axis=0
along the rows (namely, index in pandas), and axis=1
along the columns. For added clarity, one may choose to specify axis='index'
(instead of axis=0
) or axis='columns'
(instead of axis=1
).
JavaScript
1
9
1
+------------+---------+--------+
2
| | A | B |
3
+------------+---------+---------
4
| 0 | 0.626386| 1.52325|----axis=1----->
5
+------------+---------+--------+
6
| |
7
| axis=0 |
8
↓ ↓
9