筛选为NaN的布尔值,可接受单个标量或者数组
筛选stu_name
为NaN的所有行:
df = pd.DataFrame({'stu_name': ['Tom', 'Tony', 'Jack', 'Jack', np.nan], 'stu_age': [16, 16, 15, np.nan, 21]}) print(df) df1 = df[df['stu_name'].isna()] print(df1)
对NaN值进行填充,官方文档
标量/dict/Series/DataFrame
value
参数指定的值来填充(默认方式)-
df = pd.DataFrame({'stu_name': ['Tom', 'Tony', 'Jack', 'Jack', np.nan], 'stu_age': [16, 16, 15, np.nan, 21]}) print(df) df1 = df.fillna('-') print(df1)
stu_name
为NaN的填充为UNKNOWN
,stu_age
为NaN的填充为-1
df = pd.DataFrame({'stu_name': ['Tom', 'Tony', 'Jack', 'Jack', np.nan], 'stu_age': [16, 16, 15, np.nan, 21]}) print(df) df2 = df.fillna({'stu_name': 'UNKNOWN', 'stu_age': -1}) print(df2)