Python 基于dataframe中另一列的值,使用lambda if条件设置列 让我们考虑这样一个数据框: (index) condition number 0 'increase' 1 1 'do-nothing' 1

Python 基于dataframe中另一列的值,使用lambda if条件设置列 让我们考虑这样一个数据框: (index) condition number 0 'increase' 1 1 'do-nothing' 1,python,pandas,lambda,Python,Pandas,Lambda,如果同一行中的条件元素等于“增加”,我想增加数字列中的元素。原则上,我也想到了类似的事情: # non-working example df['number'] = df['number'].apply(lambda x: x+=1 if x['condition'] == 'increase' else x) 这个例子当然不起作用。有什么有效的方法吗?我想你正在寻找: df.loc[df['condition'] == 'increase', 'number'] += 1

如果同一行中的
条件
元素等于“增加”,我想增加
数字
列中的元素。原则上,我也想到了类似的事情:

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

这个例子当然不起作用。有什么有效的方法吗?

我想你正在寻找:

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