简体   繁体   中英

Using lambda if condition to column based on value of another column in Pandas dataframe

Let's consider a dataframe like this:

(index)    condition    number
0          'increase'   1
1          'do-nothing' 1

I'd like to increment the elements in the number column if the condition element in the same row is equal to 'increase'. In principle, I thought about something similar:

# non-working example
df['number'] = df['number'].apply(lambda x: x+=1 if x['condition'] == 'increase' else x)

This example of course does not work. Is there an efficient method to do it?

I think you're looking for:

df.loc[df['condition'] == 'increase', 'number'] += 1

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