简体   繁体   中英

change cell value based on condition

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM