Ashwini Maddala
Ashwini Maddala

Reputation: 197

Calculate percentage share within rows of dataframe

I have a dataframe with various states GDP data divided in multiple sectors. I am trying to get the percentage contribution of primary, secondary and tertiary sectors as a percentage of total GDP for all the states. Below is the dataframe and I am not sure how I can approach towards this. DataFrame

Below are the results I am trying to achieve:

Primary % Contribution = (Primary for that state/ State GSDP )* 100 
Secondary % Contribution = (Secondary for that state/ State GSDP )* 100 
Tertiary % Contribution = (Tertiary for that state/ State GSDP )* 100 

I am trying to get an output of this as below.

Expected Result

Upvotes: 0

Views: 97

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150785

You can try pivot the dataframe:

new_df = df.pivot(index='State',columns='Item', values='GSDP')
for item in ['Primary', 'Secondary']:
    new_df[item+'_pct'] = new_df[item]/new_df['Gross State'] 

new_df['Tertiary_pct'] = 1 - new_df[['Primary_pct', 'Secondary_pct']].sum(1)

Note: pivot works only if you have one row for each pair (state, item). Otherwise, consider pivot_table:

new_df = df.pivot_table(index='State',columns='Item', values='GSDP', aggfunc='sum')

Upvotes: 1

nick
nick

Reputation: 1350

The solution will pivot by the state column and then you have all of the information to calculate the percentages.

df_pivot = df.pivot(index='state', columns='item', values='GSDP')

Now you can easily calculate your percentages:

df_pivot['PrimaryPercent'] = df_pivot.Primary / df_pivot['Gross State Domestic Product'] * 100
df_pivot['SecondaryPercent'] = df_pivot.Secondary / df_pivot['Gross State Domestic Product'] * 100

etc

Upvotes: 0

Related Questions