dropna
import pandas as pd
import numpy as np
df = pd.DataFrame(
{
"name": ['Alfred', 'Batman', 'Catwoman'],
"toy": [np.nan, 'Batmobile', 'Bullwhip'],
"born": [pd.NaT, pd.Timestamp("1940-04-25"), pd.NaT]
}
)
print(df)
'''
name toy born
0 Alfred NaN NaT
1 Batman Batmobile 1940-04-25
2 Catwoman Bullwhip NaT
'''
df.dropna() # 默认 axis=0, 判断行
'''
name toy born
1 Batman Batmobile 1940-04-25
'''
df.dropna(axis=1) # 判断列
'''
name
0 Alfred
1 Batman
2 Catwoman
'''
df.dropna(axis=0, how='all') # 指定所有值全为缺失值才删除
'''
name toy born
0 Alfred NaN NaT
1 Batman Batmobile 1940-04-25
2 Catwoman Bullwhip NaT
'''
df.dropna(axis=0, thresh=2) # 指定至少出现过两个缺失值才删除
'''
name toy born
1 Batman Batmobile 1940-04-25
2 Catwoman Bullwhip NaT
'''
df.dropna(subset=['name', 'born']) # 指定哪几列中含有缺失值才删除
'''
name toy born
1 Batman Batmobile 1940-04-25
'''
标签:name,Python,Batman,dropna,born,NaT,df,pandas
From: https://www.cnblogs.com/jessecheng/p/17340442.html