程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Detailed explanation of dropna() parameter in pandas

編輯:Python

DataFrame.dropna( axis=0, how=‘any’, thresh=None, subset=None, inplace=False)
There are five parameters , Official document link
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html

1.axis Parameter determines whether to delete rows or columns that contain missing values

axis=0 or axis='index’ Delete rows with missing values ,
axis=1 or axis='columns’ Delete columns with missing values ,

df = pd.DataFrame({
"name": ['Alfred', 'Batman', 'Catwoman'],
"toy": [np.nan, 'Batmobile', 'Bullwhip'],
"born": [pd.NaT, pd.Timestamp("1940-04-25"),
pd.NaT]})
# Output 
name toy born
0 Alfred NaN NaT
1 Batman Batmobile 1940-04-25
2 Catwoman Bullwhip NaT
df.dropna()
# The default is axis=0
name toy born
1 Batman Batmobile 1940-04-25
df.dropna(axis='columns')
# Output 
name
0 Alfred
1 Batman
2 Catwoman

2.how Parameter when we have at least one NA when , Determine whether to DataFrame Delete rows or columns from
how='all’ perhaps how=‘any’.
how='all’ When, it means to delete rows with all missing values ( Column )
how='any’ When, it means to delete the row with missing value ( Column )

df.dropna(how='all')
# No, it's all NA So do not delete 
name toy born
0 Alfred NaN NaT
1 Batman Batmobile 1940-04-25
2 Catwoman Bullwhip NaT

3.thresh=n Indicates that the reservation contains at least n A non na Rows of numeric values

df.dropna(thresh=2)
# Indicates that the reservation contains at least 2 Rows without missing values , The first line has only one non missing value, so it needs to be deleted 
name toy born
1 Batman Batmobile 1940-04-25
2 Catwoman Bullwhip NaT

4.subset Define which columns to look for missing values in

df.dropna(subset=['name', 'born'])
# Delete on 'name' 'born' Column contains rows with missing values 
name toy born
1 Batman Batmobile 1940-04-25

5.inplace Means directly in the original DataFrame modify


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved