Python DataFrame - apply different calculations due to column value

I have a question about choosing and using functions in Python.

Say my DataFrame:

Col 0   Col 1  Col 2  Col 3
Good      1      3      1
Good      2      2      1
Bad       0      1      1

      

I want to create the result of another column based on the "Col 0" value:

1. Col 1 - Col 2, if Col 0 == 'Good'
2. Col 3 - Col 2, if Col 0 == 'Bad'

      

Ie:

For the first row, result = 1-3 = -2
For the third row, result = 1-1 = 0

      

How to do it?

Thank you so much!!!!!!!

+3


source to share


1 answer


You can do it using 2 calls loc

:

In [46]:

df.loc[df['Col0'] == 'Good', 'Result'] = df['Col1'] - df['Col2']
df.loc[df['Col0'] == 'Bad', 'Result'] = df['Col3'] - df['Col2']
df
Out[46]:
   Col0  Col1  Col2  Col3  Result
0  Good     1     3     1      -2
1  Good     2     2     1       0
2   Bad     0     1     1       0

      

Or np.where

:



In [48]:

df['Result'] = np.where(df['Col0'] == 'Good', df['Col1'] - df['Col2'], df['Col3'] - df['Col2'])
df
Out[48]:
   Col0  Col1  Col2  Col3  Result
0  Good     1     3     1      -2
1  Good     2     2     1       0
2   Bad     0     1     1       0

      

The version np.where

assumes that lines that are not "good" are "bad" or that you want to execute df['Col3'] = df['Col2']

instead

+1


source







All Articles