1

I would like to make a graph to show the proportion p of each element of a df.groupby result.

My data looks like this:

I would like to display a bar plot, using the df_country data.

for the country CH : p = 920/tot, DE : p = 3/tot, US : p= 287 321/ tot

with tot = sum of all elements : 920 + 3 + 287 321 = 288 244

I need to do the same with df_unit. Do I need a loop to do this, or can a simple function help me ?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Giordano
  • 37
  • 6
  • Please add any relevant information (code, input data, expected output) as text and not images, see [mcve] and https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Shaido Jan 27 '22 at 09:54
  • Welcome to Stack Overflow. What do you see as the logical steps to solving the problem? What part of this do you actually need help with? Do you know how to draw a plot at all? Can you get the `tot` value? It *sounds like* you want to know how to divide each value through by `tot` without using a loop. Is that right? The answer, in Pandas, is that you simply do one division, with **the column** on the left side and the `tot` value on the right. Being able to write that sort of thing is *the reason to use Pandas*. – Karl Knechtel Jan 27 '22 at 10:07

1 Answers1

1

You could do something like:

df_country['norm'] = df_country['nbcountry'] / df_country['nbcountry'].sum()
df_country = df_country[['norm', 'country']].set_index('country')
df_country.plot.bar()

And do the same for the other dataframe.

toni057
  • 582
  • 1
  • 4
  • 10
  • Thank you ! it's working, but i can't see on my graph the low proportion, cause of one element is predominate... you think if i modified the axis i could see the other proportion ? – Giordano Jan 27 '22 at 10:14
  • We can't decide for you what the code should do. If you need help to understand what kind of modification can be done to the axis, you might try https://math.stackexchange.com . – Karl Knechtel Jan 27 '22 at 10:16
  • In that case you might try using the logarithm scale - however you and the audience both need to be aware that the distribution you are looking at is very skewed. – toni057 Jan 27 '22 at 15:45