Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add calculated column to a pandas pivot table

Tags:

python

pandas

I have created a pandas data frame and then converted it into pivot table.

My pivot table looks like this:

Operators   TotalCB     Qd(cb)  Autopass(cb)
Aircel India    55        11       44
Airtel Ghana    20        17        3
Airtel India    41         9        9
Airtel Kenya    9          4        5
Airtel Nigeria  24        17        7
AT&T USA        18        10        8

I was wondering how to add calculated columns so that I get my pivot table with Autopass% (Autopass(cb)/TotalCB*100) just like we are able to create them in Excel using calculated field option.

I want my pivot table output to be something like below:

Operators   TotalCB     Qd(cb)  Autopass(cb)    Qd(cb)% Autopass(cb)%
Aircel India    55          11    44             20%     80%
Airtel Ghana    20          17     3             85%     15%
Airtel India    41          29     9             71%     22%
Airtel Kenya     9           4     5             44%     56%
AT&T USA        18          10     8             56%     44%

How do I define the function which calculates the percentage columns and how to apply that function to my two columns namely Qd(cb) and Autopass(cb) to give me additional calculated columns

like image 423
Pynewbie Avatar asked Oct 11 '14 10:10

Pynewbie


1 Answers

This should do it, assuming data is your pivoted dataframe:

data['Autopass(cb)%'] = data['Autopass(cb)'] / data['TotalCB'] * 100
data['Qd(cb)%'] = data['Qd(cb)'] / data['TotalCB'] * 100

Adding a new column to a dataframe is as simple as df['colname'] = new_series. Here we assign it with your requested function, when we do it as a vector operation it creates a new series.

like image 193
Korem Avatar answered Oct 09 '22 16:10

Korem