Gambler Tech
Gambler Tech

Reputation: 11

How to get percentage of data frame based on single column?

I am trying to find out the percentage of each and every cells in the dataframe based on single column. like for example

df=pd.read_csv("https://raw.githubusercontent.com/gambler2020/Data_Analysis/master/population/sample_pop.csv")
percentage=(df["Africa"]/df["World"])*100
percentage
0       5.135419
1       5.751118
2       6.392094
3       6.995521
4       7.416472
         ...    
254    16.483733
255    16.720034
256    16.958185
257    17.198624
258    17.441174
Length: 259, dtype: float64

here i got only single column. I have a large dataset then how would i get percentage of all the columns.

Upvotes: 1

Views: 38

Answers (1)

Timeless
Timeless

Reputation: 37747

You can use pandas.DataFrame.join and pandas.DataFrame.div combined to create the percentage column of each country in your dataframe. And for that, you have first to select the columns of countries (which means all the columns except Country and Year by using pandas.DataFrame.loc)

out = df.join(df.loc[:, ~df.columns.isin(['Year', 'World'])].div(df['World'], axis=0).mul(100).add_prefix('%_'))
>>> display(out.loc[:, ["%_Africa"]])

#to display the % of Africa

enter image description here

>>> display(out.iloc[: , -5:])

#to display the last five columns

enter image description here

Upvotes: 1

Related Questions