pandas.DataFrame.dropna() function with examples to remove null values

Spread the love

pandas.DataFrame.dropna(): This function used to remove null or empty values from data. It’s very helpful while working in datascience and machine learning projects.

Syntax: DataFrame.dropna(axis=0, how=’any’, thresh=None, subset=None, inplace=False)

axis : {0 or ‘index’, 1 or ‘columns’}, default 0
Determine if rows or columns which contain missing values are removed.

0, or ‘index’ : Drop rows which contain missing values.
1, or ‘columns’ : Drop columns which contain missing value.
Deprecated since version 0.23.0: Pass tuple or list to drop on multiple axes. Only a single axis is allowed.
how : {‘any’, ‘all’}, default ‘any’
Determine if row or column is removed from DataFrame, when we have at least one NA or all NA.

‘any’ : If any NA values are present, drop that row or column.
‘all’ : If all values are NA, drop that row or column.
thresh : int, optional
Require that many non-NA values.

subset : array-like, optional
Labels along other axis to consider, e.g. if you are dropping rows these would be a list of columns to include.

inplace : bool, default False
If True, do operation inplace and return None.

DataFrame with NA entries dropped from it.

Pandas.DataFrame.dropna() example :

import pandas as pd import pandas as pd df = pd.DataFrame({"name": ['david', 'dany', 'Caterine'], "salary": [None,25000,None]}) print(df) df=df.dropna() print(df)


       name   salary
0     david      NaN
1      dany  25000.0
2  Caterine      NaN
   name   salary
1  dany  25000.0



Leave a Reply

Your email address will not be published. Required fields are marked *