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

New column based on all others in pandas

I am processing a excel file with a unknown number of columns, which may or may not be filled.

I want to create a new column in the dataframe which identifies which rows contains contain a ‘nan’ in any of the columns.

The df:

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

Col-1    Col-2  ...   Col-n    has_nans
ok       nan    ...   ok       true
ok       ok     ...   ok       false
ok       ok     ...   nan      true

I have tried may variations of:

df['res'] = df.iloc[:,2:].isna().all(axis=1)

to no avail.. Maybe a way would be using a lambda like in here? Though I still couldn’t quite figure out.

thanks!

>Solution :

So we should make sure ‘nan’ is np.nan , then change the function to any

#df = df.replace({'nan':np.nan})
df['res'] = df.iloc[:,2:].isna().any(axis=1)
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