[英]Extract Value From Pandas Dataframe Based On Condition in Another Column
[英]Using lambda if condition to column based on value of another column in Pandas dataframe
讓我們考慮這樣的 dataframe:
(index) condition number
0 'increase' 1
1 'do-nothing' 1
如果同一行中的condition
元素等於“增加”,我想增加number
列中的元素。 原則上,我想到了類似的東西:
# 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.