rtorres
rtorres

Reputation: 2991

Drawing Relationships Between Pandas Dataframe Columns

I have a Pandas Dataframe that has multiple columns. There are 2 columns I'm interested in at this point, and they look something like this:

DataFrame data

I want to draw a bar graph with the row count of Yellow instances, broken down per Age as such:

Bar Graph - Yellow Color count per Age

3 instances of Yellow at Age 10, and 1 instance of Yellow at Age 15.

Can I get what I want out of Pandas, and if so how? I'm pretty new to Pandas and this knowledge domain, so any pointers are appreciated.

Upvotes: 1

Views: 2170

Answers (1)

piRSquared
piRSquared

Reputation: 294546

consider the dataframe df

y, r, b, o = 'Yellow', 'Red', 'Blue', 'Orange'
df = pd.DataFrame(dict(
    Color=[y, r, b, y, o, r, y, y],
    Age=[10, 15, 20, 10, 20, 15, 15, 10]
))

df.groupby(['Color', 'Age']).size().loc[y].plot.bar()

enter image description here


There are many ways to get at the same data.
Another example

df.groupby('Color').Age.value_counts().loc['Yellow'].plot(kind='bar')

Upvotes: 2

Related Questions