Home > Blockchain >  New column based on all others in pandas
New column based on all others in pandas

Time:10-08

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:

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!

CodePudding user response:

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)
  • Related