#Pandas对缺失值的处理 #pandas使用这些函数处理缺失值; ##isnull和notnull:检测是否是空值,可用于df和series- ##dropna:丢弃、删除缺失值 #axis:删除行还是列,{0 or 'index', 1 or "columns}, default o #how:如果等于any则任何值为空都删除,如果等于all则所有值都为空才删除- # inplace :如果为True则修改当前df,否则返回新的df ##fillna:填充空值 #value;用于填充的值,可以是单个值,或者字典(key是列名,value是值) #method :等于il 使用前一个不为空的值填充forword fill;等于bfill使用后一个不为空的值填充backword fill # axis ∶按行还是列填充,{0 or 'index', 1 or "columns'} # inplace :如果为True则修改当前df,否则返回新的df import pandas as pd #读取Excel,并忽视前2个空行 studf = pd.read_excel("D:\\python39\\pandas\\antlearnpandasmaster\\datas\\student_excel\\student_excel.xlsx",skiprows = 2) #print(studf) #检测空值 #print(studf.isnull()) #print(studf['分数'].isnull()) #print(studf.loc[studf['分数'].notnull(),:]) ##删除全是空值的列 studf.dropna(axis=1,how ='all',inplace=True) ##删除全是空值的行 studf.dropna(axis=0,how ='all',inplace=True) ##将分数列为空的值填充为0 studf['分数'].fillna(0,inplace=True) ##填充为空值的姓名,采用向前填充的方法 studf.loc[:,'姓名'] = studf['姓名'].fillna(method = 'ffill') print(studf) ##将清洗好的数据进行保存 studf.to_excel("D:\\python39\\pandas\\antlearnpandasmaster\\datas\\student_excel\\student_excel_clean1.xlsx",index = False)