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