theUnknown
theUnknown

Reputation: 283

Dataframe change existing columns values to a single particular value

have a df with values

df

0      1
sun   NaN
moon  123
cat   NaN
dog   yatch

Turn the values that are already present to pass and NaN to fail

expected Output

0      1
sun   fail
moon  pass
cat   fail
dog   pass

Upvotes: 2

Views: 43

Answers (2)

Christian Sloper
Christian Sloper

Reputation: 7510

A variation without numpy:

mask = df[1].isna()
df.loc[mask, 1] = 'fail'
df.loc[~mask,1] = 'pass'

Upvotes: 0

jezrael
jezrael

Reputation: 862611

Use numpy.where with Series.isna:

df[1] = np.where(df[1].isna(), 'fail', 'pass')

Upvotes: 5

Related Questions