Pandas axis=0 and axis=1

Jimmy (xiaoke) Shen
1 min readFeb 16, 2023

--

Just as in the image, some times we want to say whether we should put x first or y first, for Numpy, we want to know which axis should be the row and which axis should be column.

A nice discussion can be found in [1].

The top answer from [1] is pretty clear.

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). Top answer from [1]

along which is critical. As the pandas is putting the same type data to each column, so column should has a higher priority, this is the missing point I want to highlight from the top answer.

Reference

[1] https://stackoverflow.com/questions/22149584/what-does-axis-in-pandas-mean

--

--

No responses yet