How to drop rows of Pandas DataFrame whose value in a certain column is NaN?

 

You can drop rows of a Pandas DataFrame whose value in a certain column is NaN using the dropna() method. Here's an example:

python
import pandas as pd # Create a sample DataFrame data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Age': [25, None, 30, 22]} df = pd.DataFrame(data) print("Original DataFrame:") print(df) # Drop rows where the 'Age' column has NaN values df_cleaned = df.dropna(subset=['Age']) print("\nDataFrame after dropping rows with NaN values in 'Age' column:") print(df_cleaned)

Output:

sql
Original DataFrame: Name Age 0 Alice 25.0 1 Bob NaN 2 Charlie 30.0 3 David 22.0 DataFrame after dropping rows with NaN values in 'Age' column: Name Age 0 Alice 25.0 2 Charlie 30.0 3 David 22.0

In this example:

  • We create a sample DataFrame with the 'Name' and 'Age' columns.
  • The dropna() method is used to drop rows where the value in the 'Age' column is NaN. The subset parameter specifies the column(s) to consider for NaN checking.
  • The resulting DataFrame df_cleaned contains only the rows where the 'Age' column does not have NaN values.

Remember that the dropna() method by default returns a new DataFrame with the NaN rows dropped, leaving the original DataFrame unchanged. If you want to modify the original DataFrame in-place, you can use the inplace=True parameter:

python
df.dropna(subset=['Age'], inplace=True)

Comments