Pandas DataFrame - calculated column based on subset of columns

I have the following DataFrame

                           Qtr Premium      Claim     Rate

Type    Code                                           
A        3                  14  3552.77      100991.7  0.004017
         3                  15  5610.67      105763.6  0.004017
         3                  16  6463.22      107740.6  0.004017
         4                  17  6129.91      106967.7  0.005638
         4                  18  4688.65      103625.6  0.005638
         4                  19  2158.94      97759.66  0.005638
         4                  20  8540.77      89369.72  0.005638

      

I have a constant "c"

I want to do a row-by-row calculation that uses the corresponding values ​​from Qtr and Rate, but updates the Premium and Claim values.

Example:

Premium = Premium / (1+Rate)^(c-Qtr)
Claim = Claim / (1+Rate)^(c-Qtr)

      

I actually have many columns in which I want this calculation to be done.

+3


source to share


1 answer


When df is the name of your dataframe and c is your constant, try:

df['Premium'] = df.Premium / ( 1 + df.Rate ) ** (c - df.Qtr)
df['Claim'] = df.Claim / ( 1 + df.Rate ) ** ( c - df.Qtr )

      

Update to comment, I'm sure there is a more pythonic way to do this, but this works:



columns = df.columns
subset_cols = columns.drop(['Rate','Qtr'])
for col in subset_cols:
    df[col] = df[col] / ( 1 + df.Rate ) ** (c- df.Qtr)

      

Second update, you can extract the computation into a function and do the process in a list comprehension

def calc(df, col, c):
    df[col] = df[col] / ( 1 + df.Rate ) ** ( c - df.Qtr )
[calc(df, col, c) for col in df.columns.drop(['Rate','Qtr'])]

      

0


source







All Articles