-2

I know this is very simple but I don't truly know how to do it as I don't usually plot and it's giving me headache. I have two particular problems:

1ΒΊ Let's imagine I just have an column of a dataframe (or let's say, and array) with a categorical(object) variable and I would like to make a Bar Chart with the number of observations that have the different labels of that chart. For example if I have that dataframe with this column named color I would like something like this.(click on the image below)

    COLOR
0   green
1   red
2   green
3   yellow
4   pink
5   red
6   blue

Here you have an image of what I would like to get

r-beginners
  • 31,170
  • 3
  • 14
  • 32

1 Answers1

0

If you have the data in a dataframe, or a dataframe column, you could use the built-in df.plot() or df.hist() functions:

import pandas as pd
# The data
data = {'COLOR': ['green', 'red', 'green', 'yellow', 'pink', 'red', 'blue']}
df = pd.DataFrame(data)

# The plot
df['COLOR'].value_counts().plot(kind='bar')
# or, more straight forward : 
df['COLOR'].hist() 

which yields the expected output: enter image description here

Mat.B
  • 336
  • 2
  • 8
  • Or maybe `df_counts = df['COLOR'].value_counts(); df_counts.plot(kind='bar', color=df_counts.index, rot=0)`? – JohanC Jan 03 '23 at 13:55
  • yes, most definitely, but I wasn't sure the column would always be a 'color'! – Mat.B Jan 04 '23 at 08:47