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