简体   繁体   中英

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.

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

在此处输入图像描述

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

#to display the last five columns

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM