Describe an intuitive way to understand axis parameters in Pandas and Numpy.

The sort, mean, and drop operations in Numpy and Pandas are not defined by a method, but by a method specified by axis. For example:

We first inhereDownload a CSV file of alcohol consumption in each country as an example.



For pandas, axis 0 drops row and axis 1 drops column. For pandas, axis 1 drops column.

But what about mean operations?

As you can easily see, Axis 0 yields the mean of each column, while Axis 1 yields the mean of each row. What about in Numpy?

It is easy to see that the sum for each row is obtained when axis is 1, and the sum for each column is obtained when axis is 0.

From the above example, it seems that axis 1 represents operations in the horizontal direction, and axis 0 represents operations in the vertical direction. For example, the sum of axis 1 is the sum of each line.

In Pandas’ Dataframe, why does axis=1 represent dropping the entire column? The following example can also illustrate some of the situation:

How do I use the “axis” parameter in pandas? – YouTube, you can also draw some conclusions, the author says:

0 is the row axis, and 1 is the column axis. When you drop with axis=1, that means drop a column. When you take the mean with axis=1, That means the operation should “move across” the column axis, which produces row means. “0 is the axis of the column, and 1 is the axis of the column. When axis=1 is dropped, a column will be dropped, and axis=1 is used to get mean, so this operation ‘traverses’ the axis of the column and generates mean on the row”.

And, in fact, we can do it this way,



Axis =0 is the same as axis= ‘rows’.