Python - Remove the missing (NaN) values in the DataFrame

PythonServer Side ProgrammingProgramming

To remove the missing values i.e. the NaN values, use the dropna() method. At first, let us import the required library −

import pandas as pd

Read the CSV and create a DataFrame −

dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\CarRecords.csv")

Use the dropna() to remove the missing values. NaN will get displayed for missing values after dropna() is used −

dataFrame.dropna()

Example

Following is the complete code

import pandas as pd

# reading csv file
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\CarRecords.csv")
print("DataFrame with some NaN (missing) values...\n",dataFrame)

# count the rows and columns in a DataFrame
print("\nNumber of rows and column in our DataFrame = ",dataFrame.shape)

# drop the missing values
print("\nDataFrame after removing NaN values...\n",dataFrame.dropna())

Output

This will produce the following output −

DataFrame with some NaN (missing) values...
          Car        Place   UnitsSold
0        Audi    Bangalore        80.0
1     Porsche       Mumbai         NaN
2  RollsRoyce         Pune       100.0
3         BMW        Delhi         NaN
4     Mercedes   Hyderabad        80.0
5  Lamborghini  Chandigarh        80.0
6         Audi      Mumbai         NaN
7     Mercedes        Pune       120.0
8  Lamborghini       Delhi       100.0

Number of rows and colums in our DataFrame = (9, 3)

DataFrame after removing NaN values ...
           Car       Place   UnitsSold
0         Audi   Bangalore        80.0
2   RollsRoyce        Pune       100.0
4     Mercedes   Hyderabad        80.0
5  Lamborghini  Chandigarh        80.0
7     Mercedes        Pune       120.0
8  Lamborghini       Delhi       100.0
raja
Published on 27-Sep-2021 13:50:53
Advertisements