We can use the DataFrame.dropna() function to drop missing values from a DataFrame using the pandas Python library. In this article, we will discuss:
- How to drop rows that contain missing values from a DataFrame using pandas Python library?
- How to drop columns that contain missing values from a DataFrame using pandas Python library?
How to drop rows that contain missing values from a DataFrame using pandas Python library?
We can use the following Python code to drop rows that contain missing values from a DataFrame.
import pandas
import numpy
list1 = [[1, 2, 3, 4], [5, 6, 7, 8], [9, numpy.nan, 11, 12], [numpy.nan, 14, 15, 16], [numpy.nan, 18, 19, numpy.nan]]
df1 = pandas.DataFrame(list1, index=["Row 1", "Row 2", "Row 3", "Row 4", "Row 5"], columns=["Column 1", "Column 2", "Column 3", "Column 4"])
print("df1: \n", df1)
# Drop rows that contain missing values
df2 = df1.dropna(axis=0, inplace=False)
print("df2: \n", df2)
The output of the above program will be:








































0 Comments