remove a row if all columns have contain nans in python

You can use the dropna() function in pandas to remove rows if they contain NaNs in all columns of the dataframe. Here is an example:

main.py
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3, np.nan],
                   'B': [4, np.nan, 6, np.nan],
                   'C': [7, 8, np.nan, np.nan]})

# Before removing rows
print(df)

# Drop rows that contain NaNs in all columns
df.dropna(how='all', inplace=True)

# After removing rows
print(df)
309 chars
15 lines

Output:

main.py
     A    B    C
0  1.0  4.0  7.0
1  2.0  NaN  8.0
2  3.0  6.0  NaN
3  NaN  NaN  NaN

     A    B    C
0  1.0  4.0  7.0
1  2.0  NaN  8.0
2  3.0  6.0  NaN
154 chars
11 lines

In this example, we create a Pandas DataFrame with NaN values in many of the rows. We then use dropna() function to remove all the rows with NaN values a using how='all' parameter to ensure that only rows with all NaN values are removed. We use inplace=True argument to modify the original DataFrame.

related categories

gistlibby LogSnag