[英]Using lambda if condition to column based on value of another column in Pandas dataframe
Let's consider a dataframe like this:让我们考虑这样的 dataframe:
(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'.如果同一行中的condition
元素等于“增加”,我想增加number
列中的元素。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.