Home > Mobile >  Percentage calculation for the whole df in Python
Percentage calculation for the whole df in Python

Time:09-24

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

CodePudding user response:

This would be my take on it:

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

CodePudding user response:

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
  • Related