In Python, you can remove missing values (NaN or null values) from a Pandas dataframe using the dropna() method. The dropna() method removes any rows or columns that contain missing values based on the specified axis.
Here's an example of how to use the dropna() method to remove missing values from a Pandas dataframe:
import pandas as pd |
This will output a new dataframe with the missing values removed:
name age gender 0 John 30.0 M 3 Susan 40.0 F |
In this example, the dropna() method has removed the rows with missing values from the age and gender columns.
By default, the dropna() method removes any row that contains at least one missing value. You can also specify the axis=1 parameter to remove any column that contains at least one missing value:
# remove columns with missing values from the dataframedf_cleaned = df.dropna(axis=1)print(df_cleaned) |
This will output a new dataframe with the age and gender columns removed:
name 0 John 1 Jane 2 Mike 3 Susan |
In this example, the dropna() method has removed the age and gender columns because they both contain missing values.