Skip to content
Advertisement

Showing the proportions of values across each column in a DataFrame in Python

I have created the following DataFrame:

JavaScript

Now I wish to show the proportion of each value (0,1,2) across each column. Ideally I’d like to represent this as a stacked bar chart – Column names on the x axis (so 8 bars in total from A to H), with the different colours on the bars representing the proportion of each value (0,1,2).

What’s the easiest/simplest/most concise way to do this?

Edit: I’ve found an easy way to represent the proportions – not as a bar char, but as a DataFrame. See below:

JavaScript

However, is there a more concise way to code this? E.g. anyway to make the above code into a loop?

Advertisement

Answer

This seems to be the most efficient way. In terms of shortening it, is it this what you are looking for? It is really your solution, just condensed via comprehensions.

JavaScript

which results in

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement