collarblind
collarblind

Reputation: 4739

Plotting Pandas Dataframe by Type

This is the plot i have right now.

np.random.seed(1234)
test = pd.DataFrame({'id':[1,1,2,2,3,3,4,4],
                     'score':np.random.uniform(0,1,8),
                    'type': [0,1,0,1,0,1,0,1]})

test

    id     score  type
0   1  0.191519     0
1   1  0.622109     1
2   2  0.437728     0
3   2  0.785359     1
4   3  0.779976     0
5   3  0.272593     1
6   4  0.276464     0
7   4  0.801872     1

test.plot(x='id',y='score',kind='bar')

enter image description here

How do I plot it so it groups it by the 'type' column and a different color for each type? For example 'type' 1, i want the bars to be next to each other and color red for 'type 1 and color blue for 'type 0' so it's easier for comparison.

Upvotes: 1

Views: 116

Answers (1)

Alexander
Alexander

Reputation: 109546

Assuming you want to sum your groupings, would this work? If not, you can probably play around with it to achieve your objectives. If not, please post a table of the summary result that you'd like to graph.

test.groupby(['id', 'type']).sum().unstack().plot(kind='bar')

enter image description here

Upvotes: 2

Related Questions