I have the following dataframe

import pandas as pd
d = {

    'ID':[1,2,3,4,5],
    'Price1':[5,9,4,3,9],
    'Price2':[9,10,13,14,18],
     'Type':['A','A','B','C','D'],


}
df = pd.DataFrame(data = d)
df

For applying the formula without condition I use the following code

df = df.eval(
 'Price = (Price1*Price1)/2'
)
df

How to do the formulas without splitting the dataframe which had different conditions

Need a new column called Price_on_type

The formula is differing for each type

For type A the formula for Price_on_type = Price1+Price1

For type B the formula for Price_on_type = (Price1+Price1)/2

For type C the formula for Price_on_type = Price1

For type D the formula for Price_on_type = Price2

Expected Output:

import pandas as pd
d = {

    'ID':[1,2,3,4,5],
    'Price1':[5,9,4,3,9],
    'Price2':[9,10,13,14,18],
    'Price':[12.5,40.5, 8.0, 4.5, 40.5],
     'Price_on_type':[14,19,8.0,3,18],


}
df = pd.DataFrame(data = d)
df

2 Answers

2
jezrael On Best Solutions

You can use numpy.select:

masks = [df['Type'] == 'A',
         df['Type'] == 'B',
         df['Type'] == 'C',
         df['Type'] == 'D']

vals = [df.eval('(Price1*Price1)'),
        df.eval('(Price1*Price1)/2'),
        df['Price1'],
        df['Price2']]

Or:

vals = [df['Price1'] + df['Price2'],
        (df['Price1'] + df['Price2']) / 2,
        df['Price1'],
        df['Price2']]

df['Price_on_type'] = np.select(masks, vals)
print (df)
   ID  Price1  Price2 Type  Price_on_type
0   1       5       9    A           14.0
1   2       9      10    A           19.0
2   3       4      13    B            8.5
3   4       3      14    C            3.0
4   5       9      18    D           18.0
1
andy On

If your data is not too big, using apply with custom function on axis=1

def Prices(x):
    dict_sw = {
            'A': x.Price1 + x.Price2,
            'B': (x.Price1 + x.Price2)/2,
            'C': x.Price1,
            'D': x.Price2,
            }
    return dict_sw[x.Type]

In [239]: df['Price_on_type'] = df.apply(Prices, axis=1)

In [240]: df
Out[240]:
   ID  Price1  Price2 Type  Price_on_type
0   1       5       9    A           14.0
1   2       9      10    A           19.0
2   3       4      13    B            8.5
3   4       3      14    C            3.0
4   5       9      18    D           18.0

Or using the trick convert True to 1 and False to 0

df['Price_on_type'] = \
     (df.Type == 'A') * (df.Price1 + df.Price2) + \
     (df.Type == 'B') * (df.Price1 + df.Price2)/2 + \
     (df.Type == 'C') * df.Price1 + \
     (df.Type == 'D') * df.Price2

Out[308]:
   ID  Price1  Price2 Type  Price_on_type
0   1       5       9    A           14.0
1   2       9      10    A           19.0
2   3       4      13    B            8.5
3   4       3      14    C            3.0
4   5       9      18    D           18.0