import pandas as pd
fpath = './ant-learn-pandas-master/datas/student_excel/student_excel.xlsx'
#skiprows忽略两个空行
rating = pd.read_excel(fpath,skiprows=2)
print(rating)
Detection:
#检测所有格是否为空值
print(rating.isnull())
#或者
print(rating.notnull())
Remove empty values:
#删掉全是空值的列
rating.dropna(axis='columns', how='all', inplace=True)
#删掉全是空值的行
rating.dropna(axis='index', how='all', inplace=True)
Filling: Fill
the fraction of NaN to 0
rating.loc[:, '分数'] = rating.loc[:, '分数'].fillna(0)
Fill in the name of NaN with the previous name
#用前面的填充空值姓名
rating.loc[:, '姓名'] = rating.loc[:, '姓名'].fillna(method='ffill')
Save the cleaned excel
rating.to_excel('路径',index=False)
sh is the condition, use the bool value to determine whether the condition is met,
astype is used to convert the Series type to the string type
sh = rating['分数'].astype(str).str.startswith('0')
print(rating[sh == True])