Pandas mathematical operation, conditional on column value

前端 未结 5 1230
自闭症患者
自闭症患者 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:04

    It was throwing ValueError since you were not using the loc correctly. Here is the solution using loc:

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

    Output:

     col1 col2 col3 math
    0    A   2   0    NaN
    1    A   1   1    NaN
    2    B   9   9    19.0
    3    NaN 8   4    NaN
    4    D   7   2    NaN
    5    C   4   3    NaN
    
    0 讨论(0)
  • 2021-01-19 09:08

    where

    I perform the math then mask it using pandas.Series.where by passing the boolean series df.col1.eq('B')

    df.assign(math=df.col3.add(10).where(df.col1.eq('B')))
    
      col1  col2  col3  math
    0    A     2     0   NaN
    1    A     1     1   NaN
    2    B     9     9  19.0
    3  NaN     8     4   NaN
    4    D     7     2   NaN
    5    C     4     3   NaN
    
    0 讨论(0)
  • 2021-01-19 09:09

    Using loc

    df['math'] = df.loc[df.col1.eq('B'), 'col3'].add(10)
    
      col1  col2  col3  math
    0    A     2     0   NaN
    1    A     1     1   NaN
    2    B     9     9  19.0
    3  NaN     8     4   NaN
    4    D     7     2   NaN
    5    C     4     3   NaN
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-19 09:15

    Use:(Not a safe way to achieve it see the comment below )

    df['New']=df.col3[df.col1=='B']+10
    df
    Out[11]: 
      col1  col2  col3   New
    0    A     2     0   NaN
    1    A     1     1   NaN
    2    B     9     9  19.0
    3  NaN     8     4   NaN
    4    D     7     2   NaN
    5    C     4     3   NaN
    

    Update

    pd.concat([df,(df.col3[df.col1=='B']+10).to_frame('New')],1)
    Out[51]: 
      col1  col2  col3   New
    0    A     2     0   NaN
    1    A     1     1   NaN
    2    B     9     9  19.0
    3  NaN     8     4   NaN
    4    D     7     2   NaN
    5    C     4     3   NaN
    
    0 讨论(0)
提交回复
热议问题