一、读取excel数据
数据内容如下:

二、读取excel数据
df = pd.read_excel(data_path,skiprows=2)
ps:使用skiprows过滤掉头2行

三、isnull 判断是否为空
df.isnull()

四、notnull 判断是否非空
df.notnull()

df['name'].notnull()

五、notnull 判断指定列是否非空
df.loc[df['score'].notnull(), :]

六、dropna 删除全为空的列
# 删除全是空的列
df.dropna(axis='columns', how='all', inplace=True)

七、dropna 删除全为空的行
# 删除全是空的行
df.dropna(axis='index',how='all',inplace=True)

八、fillna 缺失值填充
df.fillna({'score':0})

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

九、fillna按照前后的元素进行填充
df.loc[:,'name'] = df['name'].fillna(method='ffill')
# ffill forward fill,按照前一个填充
# bfill back fill,按照后一个填充

十、保存结果到excel
df.to_excel('datas/student_excel/student_excel_clean.xlsx', index=False)
网友评论