105k views
5 votes
How to remove rows with na in pandas

1 Answer

0 votes

Final answer:

To remove rows with NA values in a Pandas DataFrame, the dropna() method is used. This can be customized to drop rows with any or all missing values, or just missing values in specific columns.

Step-by-step explanation:

To remove rows with NA (not available) values in Pandas, you can use the dropna() method. This method will drop all rows that contain any missing values. The basic usage of dropna() is straightforward:

import pandas as pd

df = pd.DataFrame({
'A': [1, 2, NA, 4],
'B': [NA, 2, 3, 4],
'C': [1, NA, NA, 4]
})

clean_df = df.dropna()

After executing the above code, clean_df will only have rows that do not contain any NA values. If you need to remove rows where all values are NA, you can use the how='all' argument. Alternatively, if you wish to remove rows based on NA in a specific column, use the subset=['column_name'] argument.

User Lemon Cat
by
8.7k points