delete every row where there are more than one non-missing values in python

We can delete the rows containing more than one non-missing value as follows:

main.py
import pandas as pd

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

# Count non-missing values in each row using 'count()' method  
df['non_missing'] = df.count(axis=1)

# Remove rows containing more than one non-missing values
df = df[df['non_missing'] <= 1].drop(columns=['non_missing'])

print(df)
347 chars
12 lines

This code creates a DataFrame with 4 rows and 3 columns, where some of the values are missing. Then, it counts the number of non-missing values in each row and appends a new column 'non_missing' to the DataFrame. Finally, it selects only the rows where the 'non_missing' column is less than or equal to 1 and drops the temporary column.

The resulting output will be:

main.py
     A    B    C
0  1.0  NaN  NaN
1  2.0  4.0  NaN
51 chars
4 lines

Note that this approach assumes that only numerical values are missing in the DataFrame. If there are other types of missing values, they may not be counted as such.

related categories

gistlibby LogSnag