Pandas mathematical operation, conditional on column value

前端 未结 5 1231
自闭症患者
自闭症患者 2021-01-19 08:51

I need to make a mathematical operation which is conditional on the value in a second column. Here is the setup.

Given a simple dataframe (df):

5条回答
  •  南方客
    南方客 (楼主)
    2021-01-19 09:09

    I was also able to do the following...

    df['math'] = 10 + df.loc[df['col1'] == 'B']['col3']  
    

    Which is a variation on @user3483203 answer above. Ultimately, my 'B' is a variable, so I modified for @RafaelC 's comments.

提交回复
热议问题