Calculate percentage of a column in Pandas Python

This is a very interesting tutorial to obtain the percentage of a particular column in Pandas dataframe in Python.  With the help of this method, we can visualize what percentage of a particular value is there in a specific column of a Pandas dataframe. We have to simply use a basic mathematical calculation to calculate the percentage of values in a specific column, where we have to divide a value by the sum of all the values in that column and then multiply it by 100. So, read the text below to learn and explore new ways of coding.

You can install the Pandas module using the following command into your Powershell

pip install pandas

Example 1: percentage of a column in Pandas

In this example below, we will calculate the percentage of the Salary column in the following Pandas dataframe.

# import the library
import pandas as pd
# make the raw dataframe
dataframe={'Employee':['Vivek Kaira','Nimish Adhikari','Nikunj Jatayu','John Writer','Kale Neesham','Teddy Oscar'],
          'Designation':['Senior Executive','Manager','Gamer','Software Engineer','Developer','Tea Seller'],
          'Salary':[100000000,100000,100000,1000000,10000000,10000],
          'Workers':[500,100,10,200,300,0],
          'Profit Made':[2000000000,3000000,120000,100000,4000000,100000]}
# Create the pandas dataframe
df=pd.DataFrame(dataframe)
# formulate the percentage of salary each employee gets
df['Percentage_sal']=(df['Salary']/df['Salary'].sum())*100
# display the dataframe
display(df)

Output:

percentage of a column in Pandas

Example 2: Pandas percentage calculation of a column

Similarly, we can calculate the percentage of multiple columns of a particular Pandas dataframe using the same method as shwon in the code below.

# import the library
import pandas as pd
# make the raw dataframe
dataframe={'Employee':['Vivek Kaira','Nimish Adhikari','Nikunj Jatayu','John Writer','Kale Neesham','Teddy Oscar'],
          'Designation':['Senior Executive','Manager','Gamer','Software Engineer','Developer','Tea Seller'],
          'Salary':[100000000,100000,100000,1000000,10000000,10000],
          'Workers':[500,100,10,200,300,0],
          'Profit Made':[2000000000,3000000,120000,100000,4000000,100000]}
# Create the pandas dataframe
df=pd.DataFrame(dataframe)
# formulate the percentage of salary each employee gets
df['Percentage_sal']=(df['Salary']/df['Salary'].sum())*100
# Formulate the percentage of workers
df['Percentage_worker']=(df['Workers']/df['Workers'].sum())*100
# Formulate the percentage of profit made
df['Percentage_profit']=(df['Profit Made']/df['Profit Made'].sum())*100
# display the dataframe
display(df)

Output:

percentage of a column in Pandas

 

Leave a Reply

Your email address will not be published. Required fields are marked *