8.Pandas对缺失值的处理(Excel表清洗格式异常的数据)

`#读取目标excel表,忽略前面2行
df=pd.read_excel(r'F:\人工智能_优学\Pandas-learn-code\pandas-learn-code\datas\student_excel\student_excel.xlsx',skiprows=2)

去掉所有为NaN的列,how是全部,inplace是否替换掉

df.dropna(axis='columns',how='all',inplace=True)

去掉所有为nana的行

df.dropna(axis='index',how='all',inplace=True)

将分数为NaN的值,改为0分

df.fillna({'分数':0})

也可以

df.loc[:,'分数']=df['分数'].fillna(0)

使用前面的有效值填充用ffill;forward fill,利用后面的有效值填充则bfill,即backword fill

df.loc[:,'姓名']=df['姓名'].fillna(method='ffill')

将清洗后的Excel表进行保存

df.to_excel(r'F:\人工智能_优学\Pandas-learn-code\pandas-learn-code\datas\student_excel\student_excel_clean_2.xlsx',index=False)`

posted @ 2020-07-20 20:26  hisweetyGirl  阅读(554)  评论(0编辑  收藏  举报