Jishan
Jishan

Reputation: 1684

Summing values in the same column based on conditions

I have a data frame "Country" which looks like this:

Country
1
2
1
3
4
5
4
2
2
3
4
4
3
4
5

I calculate the percentages of each of the values in the column using the code below:

percent = df_test['Country'].value_counts(normalize=True) * 100

The code above gives me the individual percentages of the values 1 until 5.

However, I would like to calculate the summed percentages for 1 and 3 together. The remaining values should be separately summed together.

Any leads on how this could be done are appreciated.

Thanks a lot.

Upvotes: 0

Views: 21

Answers (1)

Aven Desta
Aven Desta

Reputation: 2443

So how come this doesn't work? @Jishan

ratio_1 = df_test['Country'].value_counts(normalize=True)['1']
ratio_3 = df_test['Country'].value_counts(normalize=True)['3']
percent_1_3 = (ratio_1 + ratio_3)*100

Upvotes: 1

Related Questions