Home > Back-end >  change cell value based on condition
change cell value based on condition

Time:07-23

username     status      debt
 John        pending     $1000
 Mike        pending     $0
 Daymond     cleared     $0

How can I change the status of pending to cleared if the status is pending and debt is 0?

CodePudding user response:

You can use

m = df['status'].eq('pending') & df['debt'].eq('$0')

df.loc[m, 'status'] = 'cleared'
# or
df['status'] = df['status'].mask(m, 'cleared')
# or
df['status'] = np.where(m, 'cleared', df['status'])
print(df)

  username   status   debt
0     John  pending  $1000
1     Mike  cleared     $0
2  Daymond  cleared     $0

CodePudding user response:

You can use panda.loc.

df.loc[((df["status"] == 'pending') & (df["debt"] == '$0')), 'status'] = 'cleard'
print(df)

  username   status   debt
0     John  pending  $1000
1     Mike  cleared     $0
2  Daymond  cleared     $0
  • Related