根据官方的说法,1表示横轴,方向从左到右;0表示纵轴,方向从上到下。当axis=1时,数组的变化是横向的,而体现出来的是列的增加或者减少。
axis=0代表跨行(down),而axis=1代表跨列(across)。
轴用来为超过一维的数组定义的属性,二维数据拥有两个轴:第0轴沿着行的垂直往下,第1轴沿着列的方向水平延伸。
换句话说:
- 使用0值表示沿着每一列或行标签/索引值向下执行方法
- 使用1值表示沿着每一行或者列标签横向执行对应的方法
import numpy as np #创建二维数组 arr2d = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) #沿第0轴方向最大值 print(arr2d.max(0))
import pandas as pd #创建二维数组 df = pd.DataFrame([[1,2,3,4],[5,6,7,8],[9,10,11,12]],columns=["col1", "col2", "col3","col4"]) df1=df.drop("col4", axis=1) print(df1)