简体   繁体   中英

Calculate percentage of all alliances in year in dataframe pandas

I have the dataframe that looks like (real frame has years from 1990 to 2020):

Year Chains_Normalize Alliance_Name
1990 0.000 Gaint Inc
1990 0.000 Kell Inc
1990 0.000 New Corp
1991 13.222 Sad Corp
1991 18.152 Second Corp
1991 16.333 Shin Inc
1992 19.212 No inc
1992 0.000 Third inc
1992 14.332 Fourth inc

I have to calculate the percentage of alliances in every year if Chains_Normalize is not 0. (The idea is to count what percentage of all alliances from dataframe includes every year)

The result will look like this:

Year  Percentage
1990  0.000
1991  33.333
1992  22.222

1990 has all zero alliances and it's percentage is 0.

1991 has 3 no-zero alliances and (3/9)*100 = 33.333

1992 has 2 no-zero alliances and (2/9)*100 = 22.222

Simply count the non-zeros per year and then divide by the length of df and multiply by 100 (which you already do manually with (N/9)*100 ):

>>> df['Chains_Normalize'].ne(0).groupby(df['Year']).sum() / len(df) * 100
Year
1990     0.000000
1991    33.333333
1992    22.222222
Name: Chains_Normalize, dtype: float64

this works because df['Chains_Normalize'].ne(0) returns a series of booleans where the alliances are non-zero, that you can then sum:

>>> df['Chains_Normalize'].ne(0)
0    False
1    False
2    False
3     True
4     True
5     True
6     True
7    False
8     True
Name: Chains_Normalize, dtype: bool

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