# pandas的axis参数怎样理解? # axis=0 或者 "index": # 如果是单行操作,就指的是某一行 # 如果是聚合操作,指的是跨行cross rows # axis=1 或者"columns" # 如果是单列操作,就指的是某一列 # 如果是聚合操作,指的是跨列cross columns # 按哪个axis,就是这个axis要动起来,其他的axis保持不动 import pandas as pd import numpy as np df = pd.DataFrame( np.arange(12).reshape(3,4), columns = ["A","B","C","D"] ) # 1 单列drop,就是删除某一列 df.drop("A",axis=1) # 2 单行drop,及时删除某一行 df.drop(1,axis=0) # 3 按axis=0/index执行mean聚合操作 # 反直觉:输出的不会每行的结果,而是每一列的结果 # axis=0 or axis = index df.mean(axis=0) # 4 按axis=1/columns 执行mean聚合操作 df.mean(axis=1) # 在举例,加深理解 def get_sum_value(x): return x["A"] + x["B"] +x["C"] + x["D"] df["sum_value"] = df.apply(get_sum_value,axis=1) df
好好学习,天天向上