其他分享
首页 > 其他分享> > dropna 缺失数据处理

dropna 缺失数据处理

作者:互联网

pandas 官方 api

  1. 函数原型
DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)
  1. 参数意义
  1. 样例
df = pd.DataFrame({"name": ['Alfred', 'Batman', 'Catwoman'],
                   "toy": [np.nan, 'Batmobile', 'Bullwhip'],
                   "born": [pd.NaT, pd.Timestamp("1940-04-25"),
                            pd.NaT]})       
	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

删除所有存在NAN值的列

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

删除name,toy列为空的行

df.dropna(subset=['name', 'toy'])
       name        toy       born
1    Batman  Batmobile 1940-04-25
2  Catwoman   Bullwhip        NaT
df.dropna(inplace=True)
     name        toy       born
 1  Batman  Batmobile 1940-04-25

标签:25,toy,name,Batman,dropna,NaT,数据处理,缺失
来源: https://blog.csdn.net/weixin_43745072/article/details/112969660