Pandas API:dropna函数 删除无效值

函数介绍

函数举例

>>>df = pd.DataFrame({"name": ['Alfred', 'Batman', 'Catwoman'],
...                   "toy": [np.nan, 'Batmobile', 'Bullwhip'],
...                   "born": [pd.NaT, pd.Timestamp("1940-04-25"),
...                            pd.NaT]})


>>>df
       name        toy       born
0    Alfred        NaN        NaT
1    Batman  Batmobile 1940-04-25
2  Catwoman   Bullwhip        NaT


>>>df.dropna()
     name        toy       born
1  Batman  Batmobile 1940-04-25


>>>df.dropna(axis='columns')
       name
0    Alfred
1    Batman
2  Catwoman

# 设定当行中所有值都缺失时,才删除行
>>>df.dropna(how='all')
       name        toy       born
0    Alfred        NaN        NaT
1    Batman  Batmobile 1940-04-25
2  Catwoman   Bullwhip        NaT


# 只保留那些至少有2个非空值的行
>>>df.dropna(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

你可能感兴趣的:(pandas,API)