1

I have a pandas dataframe which looks like this:

Country  
 Japan   
 Japan   
 Korea   
 India   
 India   
  USA    
  USA    
  USA   

I need to count the unique values of the country column and change to percentage and need to put in the x-axis and y-axis of plotly bar chart. Can anyone teach me how to do it?

Jason
  • 23
  • 4

1 Answers1

3

Use value_counts:

df.Country.value_counts(normalize=True)
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
  • 1
    and `.mul(100)` would help :) +1 – anky Dec 18 '19 at 16:51
  • Thanks for the reply, I want to ask how to add a header for the percentage column after use the value_counts – Jason Dec 18 '19 at 17:28
  • @Jason Do you mean the column name? That's a different question entirely. – AMC Dec 18 '19 at 17:40
  • @AlexanderCécile Yes. I want to ask after I use the value_counts get the unique value. How can I use the unique value column? Most of the way I found is call the column name. Should I post this question in a new post? – Jason Dec 18 '19 at 17:53
  • 1
    @Jason Look at the docs, `value_counts()` returns a Series in this case. What you do with the Series afterwards is entirely up to you! :) – AMC Dec 18 '19 at 17:58