Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Drop rows in pandas if null values condition met

How to remove the rows if the nan values count is greater than equal (>=) to 5

pandas dataframe looks like this :

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading