1

I can transform the following dataframe:

   VALUE       COUNT  RECL_LCC  RECL_PI
0      1  15,686,114         3        1
1      2  27,537,963         1        1
2      3  23,448,904         1        2
3      4   1,213,184         1        3
4      5  14,185,448         3        2
5      6  13,064,600         3        3
6      7  27,043,180         2        2
7      8  11,732,405         2        1
8      9  14,773,871         2        3

into something like this:

RECL_PI            1           2           3
RECL_LCC                                    
1         27,537,963  23,448,904   1,213,184
2         11,732,405  27,043,180  14,773,871
3         15,686,114  14,185,448  13,064,600

by using pandas pivot table:

plot_table = LCC_PI_df.pivot_table(index=['RECL_LCC'], columns='RECL_PI', values='COUNT', aggfunc='sum')

Is there a quick way to create the pivot table with percentage of row totals instead of raw sum of counts?

2
  • Does row totals mean (27,537,963 + 23,448,904 + 1,213,184) for the first row, and so on? And you want to replace numbers in the row to percentages? Commented Oct 31, 2014 at 5:11
  • yes, that is exactly what i want. Commented Oct 31, 2014 at 5:12

1 Answer 1

3

According to comments, I think you can do that like following. Note that I converted the COUNT column to integers to do this :

#convert strings of the COUNT column to integers
import locale
locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' ) 
LCC_PI_df.COUNT = LCC_PI_df.COUNT.apply(locale.atoi)

plot_table = LCC_PI_df.pivot_table(index=['RECL_LCC'], columns='RECL_PI', values='COUNT', aggfunc='sum')
#Calculate percentages
plot_table = plot_table.apply(lambda x : x / x.sum(), axis=1)   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.