drop rows with nan pandas in python

To drop rows with NaN values in a Pandas DataFrame, you can use the dropna() method. Here's an example:

main.py
import pandas as pd

# create a sample dataframe with NaN values
df = pd.DataFrame({'A': [1, 2, np.nan, 4], 'B': [5, np.nan, np.nan, 8], 'C': [9, 10, 11, 12]})

# drop rows with NaN values and store the result in a new dataframe
df_clean = df.dropna()

# print the clean dataframe
print(df_clean)
297 chars
11 lines

This will remove any rows with NaN values from the original dataframe and create a new dataframe with only the rows that have a complete set of values. You can also specify axis=1 to drop columns with NaN values instead of rows.

related categories

gistlibby LogSnag