drop missing values in pandas in python

To drop missing values in pandas, you can use the dropna() method.

Here's an example code snippet:

main.py
# Import pandas library
import pandas as pd

# Create a sample dataframe
df = pd.DataFrame({'A': [1, 2, None, 4], 'B': [5, None, 7, 8]})

# Use dropna() to drop missing values
df = df.dropna()

# Print the updated dataframe
print(df)
234 chars
12 lines

In this example, we create a sample dataframe with some missing values. We apply the dropna() method to the dataframe to drop all rows that contain missing values (i.e. NaN values). The resulting dataframe contains only rows with complete data.

You can also use the dropna() method to drop columns instead of rows by specifying the axis parameter as 1.

main.py
# Drop columns that contain missing values
df = df.dropna(axis=1)
66 chars
3 lines

This will drop all columns containing missing values from the dataframe.

related categories

gistlibby LogSnag