DataFrame下dropna()代码示例
1.默认情况下,dropna()会删除包含缺失值的行 #这种情况下,其实是drop(axis=0,how=‘any’)
data = pd.DataFrame([[1,6.5,3],[1,None,None],
[None,None,None],[None,6.5,3]])
cleaned = data.dropna()
print("data:\n" + data)
print("cleaned: \n" + cleaned)
==Output:==
data:
0 1 2
0 1.0 6.5 3.0
1 1.0 NaN NaN
2 NaN NaN NaN
3 Nan NaN 3.0
cleaned:
0 1 2
0 1.0 6.5 3.0
2.若要删除所有值均为NaN的行,需要传入how = ‘all’
cleaned = data.dropna(how='all')
==Output:==
cleaned:
0 1 2
0 1.0 6.5 3.0
1 1.0 NaN NaN
3 NaN 6.5 3.0
3.可以用dropna(how=‘any’,axis=1)的方法删除列
cleaned = data.dropna(how='any',axis=1)
==Output:==
cleaned: