[python] pandas study notes (5) processing detection, discarding, and filling empty values for missing values

Insert picture description here


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)

Insert picture description here
Detection:

#检测所有格是否为空值
print(rating.isnull())
#或者
print(rating.notnull())

Insert picture description here
Remove empty values:

#删掉全是空值的列
rating.dropna(axis='columns', how='all', inplace=True)
#删掉全是空值的行
rating.dropna(axis='index', how='all', inplace=True)

Insert picture description here
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')

Insert picture description here
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])

Guess you like

Origin blog.csdn.net/Sgmple/article/details/113051847