简体   繁体   English

使用 lambda 如果基于 Pandas dataframe 中另一列的值的列的条件

[英]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.

相关问题 根据另一列中的条件从 Pandas 数据框中提取值 - Extract Value From Pandas Dataframe Based On Condition in Another Column 使用Python熊猫根据条件将行值复制到另一列 - Copy a row value to another column based on condition using Python pandas 根据条件将值从一列复制到另一列(使用熊猫) - Copy value from one column to another based on condition (using pandas) Pandas数据框根据查询数据框中的值选择行,然后根据列值选择其他条件 - Pandas Dataframe Select rows based on values from a lookup dataframe and then another condition based on column value 根据另一列的条件更新Pandas DataFrame中的列 - Updating a column in a Pandas DataFrame based on a condition of another column 基于具有正则表达式和 lambda 的另一列值拆分列 dataframe 中的文本 - Split text in column dataframe based on another column value with regex and lambda pandas:如果该值在第二个 dataframe 中,则根据另一个 dataframe 中的条件替换列中的值 - pandas: replace values in a column based on a condition in another dataframe if that value is in the second dataframe pandas - 使用具有 if 条件的另一列的值更新数据框列 - pandas - updating a dataframe column with value of another column with if condition 根据其他列值更改pandas DataFrame列值 - Change a pandas DataFrame column value based on another column value 根据条件使用另一个数据帧列值更新一个数据帧值 - Update one dataframe value with another dataframe column value based on the condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM