How to remove the rows if the nan values count is greater than equal (>=) to 5
pandas dataframe looks like this :
>Solution :
You could leverage isna() to identify the NaNs and then sum() those with axis=1 to get the NaN count per row. Then use that as a (negated) mask to keep the rows you want.
dfd = df.loc[~(df.isna().sum(axis=1)>=5)]
print(dfd)
Result
bl b2 b3 b4 b5 b6 b7 b8
user_id
2 1.0 3.0 4.0 2.0 5.0 7.0 8.0 6.0
4 1.0 3.0 4.0 NaN 5.0 2.0 7.0 6.0
5 NaN 3.0 2.0 NaN 4.0 1.0 5.0 NaN
