在默认情况下,只要该行含有缺失值,dropna便会删除所有包含了缺失值的行,如下所示:
data = pd.DataFrame([[1., 6.5, 3.], [1., NA, NA], [NA, NA, NA], [NA, 6.5, 3.]])
cleaned = data.dropna()
print('data\n', data, '\n')
print('cleaned\n',cleaned) # 默认删除包含缺失值的行
data
0 1 2
0 1.0 6.5 3.0
1 1.0 NaN NaN
2 NaN NaN NaN
3 NaN 6.5 3.0
cleaned
0 1 2
0 1.0 6.5 3.0
只有当传入参数how='all'
时,才会删除所有值均为NA的行,如下所示:
data.dropna(how='all')
0 1 2
0 1.0 6.5 3.0
1 1.0 NaN NaN
3 NaN 6.5 3.0
如果要用同样的方式删除列,则传入参数axis=1
,如下:
data[4] = None
data
0 1 2 4
0 1.0 6.5 3.0 None
1 1.0 NaN NaN None
2 NaN NaN NaN None
3 NaN 6.5 3.0 None
data.dropna(axis=1, how='all')
0 1 2
0 1.0 6.5 3.0
1 1.0 NaN NaN
2 NaN NaN NaN
3 NaN 6.5 3.0
过滤DataFrame的行的相关方法往往涉及时间序列数据。假如只想保留非缺失值的个数大于给定值的行,则可以用thresh参数来表示:
df = pd.DataFrame(np.random.randn(7, 3))
df.iloc[:4, 1] = NA
df.iloc[:2, 2] = NA
df
0 1 2
0 0.717847 NaN NaN
1 -0.396387 NaN NaN
2 -0.524619 NaN -0.049880
3 2.068640 NaN 0.923055
4 -0.601196 1.150763 -1.174955
5 0.277729 -1.089988 1.425802
6 0.739074 1.028694 -1.105094
df.dropna()
0 1 2
4 -0.601196 1.150763 -1.174955
5 0.277729 -1.089988 1.425802
6 0.739074 1.028694 -1.105094
df.dropna(thresh=2)
0 1 2
2 -0.524619 NaN -0.049880
3 2.068640 NaN 0.923055
4 -0.601196 1.150763 -1.174955
5 0.277729 -1.089988 1.425802
6 0.739074 1.028694 -1.105094
标签:1.0,NA,NaN,dropna,data,6.5,3.0,方法,Pandas
From: https://www.cnblogs.com/ToryRegulus/p/17133053.html