Get the percentage of a column in pandas python

Percentage of a column in pandas python is carried out using sum() function in roundabout way. Let’s see how to

  • Get the percentage of a column in pandas dataframe in python With an example

First let’s create a dataframe.


import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
     'Name':['George','Andrea','micheal','maggie','Ravi','Xien','Jalpa'],
   'Mathematics_score':[62,47,55,74,32,77,86]}

df1 = pd.DataFrame(df1,columns=['Name','Mathematics_score'])
print(df1)

df1 will be

Get the percentage of a column in pandas python 1

 

Percentage of a column in a pandas dataframe python:

Percentage of a column in pandas dataframe is computed using sum() function and stored in a new column namely percentage as shown below


df1['percentage'] = df1['Mathematics_score']/df1['Mathematics_score'].sum()
print(df1)

so resultant dataframe will be

Get the percentage of a column in pandas python 2

 

Get the percentage of a column in pandas python prev                                                                                                           Get the percentage of a column in pandas python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.