Jaroslav Kotrba
Jaroslav Kotrba

Reputation: 313

Percentage calculation for the whole df in Python

I am curious about the fastest solution for my case, what is the way to transform all the values in years 1-4 into a percentage of the Freq column with the lowest amount of code possible.

df = pd.DataFrame({'Delivery Year' : [1976,1977,1978,1979], "Freq" : [120,100,80,60],
                                              "1. Year" : [10,3,8,14], 
                                              "2. Year" : [5,float('nan'),5,float('nan')], 
                                              "3. Year" : [10,10,float('nan'),float('nan')], 
                                              "4. Year" : [13,float('nan'),float('nan'),float('nan')]
                   })
df

Thank you

Upvotes: 1

Views: 41

Answers (2)

jezrael
jezrael

Reputation: 863791

Use DataFrame.iloc for select all columns without first 2 with DataFrame.div:

df.iloc[:, 2:] = df.iloc[:, 2:].div(df['Freq'], axis=0)

print (df)
   Delivery Year  Freq   1. Year   2. Year   3. Year   4. Year
0           1976   120  0.083333  0.041667  0.083333  0.108333
1           1977   100  0.030000       NaN  0.100000       NaN
2           1978    80  0.100000  0.062500       NaN       NaN
3           1979    60  0.233333       NaN       NaN       NaN

Upvotes: 1

ql.user2511
ql.user2511

Reputation: 389

This would be my take on it:

for i in ['1. Year', '2. Year', '3. Year', '4. Year']:
    df[i] = df[i]/df['Freq']

Upvotes: 1

Related Questions