axis='columns'
关键词
axis=0
axis=1
axis='index'
axis='columns'
点击查看代码
import numpy as np
data= [
[1,2,3],
[5,4,1],
[3,2,2]
]
df = pd.DataFrame(data,columns=['A','B','C'])
f = lambda x: (x - np.min(x)) / (np.max(x) - np.min(x))
print('df',df)
df1 =df.copy()
df1 = df1.apply(f,axis=1) # 以行为单位
print(df1)
x = [1,2,3]
x1 = (x - np.min(x)) / (np.max(x) - np.min(x))
print("x1",x1)
df1 =df.copy()
df1 = df1.apply(f,axis='columns') # 以行为单位
print(df1)
输出
df A B C
0 1 2 3
1 5 4 1
2 3 2 2
A B C
0 0.0 0.50 1.0
1 1.0 0.75 0.0
2 1.0 0.00 0.0
x1 [0. 0.5 1. ]
# 证明是对df的一行数据apply函数f
A B C
0 0.0 0.50 1.0
1 1.0 0.75 0.0
2 1.0 0.00 0.0
# 证明axis='columns'和axis=1的效果是一样的
我的理解是:
- 0 or ‘index’: 将函数用于每一列,apply function to each column.
- 1 or ‘columns’: 将函数用于每一行,apply function to each row.
参考文献
pandas 中的axis:columns 与index