-1

I am a beginner of python and pandas. I am practicing the pivot_table.

This is the Data I have made for the practice.

enter image description here

DeC
  • 2,226
  • 22
  • 42
  • 4
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Dec 02 '19 at 10:28

1 Answers1

0

Assume that the source DataFrame is as follows:

    Id Status
0  747   good
1  587    bad
2  347   good
3  709   good

I think that pivot is here a bad choice. To count total values, a more natural solution is rather value_counts. Together with setting proper column names, the code can be:

res = df.Status.value_counts().reset_index()
res.columns = ['Status', 'total']

So far, we have only totals. To count percentages, another instruction is needed:

res['percentage'] = res.total / res.total.sum()

The result, for my data, is:

  Status  total  percentage
0   good      3        0.75
1    bad      1        0.25
Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41