Reputation: 361
I have the following pandas dataframe:
>>> df
>>> StartDate Port Count
2011-08-10 11:07:10 3128 10
2011-08-10 11:07:40 80 1
2011-08-10 11:07:40 443 1
2011-08-10 11:07:40 3128 10
2011-08-10 11:08:00 443 1
2011-08-10 11:08:00 3128 9
2011-08-10 11:08:20 80 1
I want to create a histogram where on x-axis will be 'StartDate' periods, on y-axis will be count and there will be one column for each value from the 'Port' column.
I tried using groupby() with df.plot.bar(), but it does not give me the result I want. How could I do it?
Ok, not the best drawing, but should give the idea. y-axis is a count, each bar represents the value from the 'Port' column. On x-axis are dates from the first column
Upvotes: 2
Views: 540
Reputation: 862431
I believe you need set_index
+ unstack
for reshaping and last use DataFrame.plot.bar
:
df1 = df.set_index(['StartDate','Port'])['Count'].unstack(fill_value=0)
print (df1)
Port 80 443 3128
StartDate
2011-08-10 11:07:10 0 0 10
2011-08-10 11:07:40 1 1 10
2011-08-10 11:08:00 0 1 9
2011-08-10 11:08:20 1 0 0
df1.plot.bar()
Alternative solution with pivot
:
df1 = df.pivot(index='StartDate', columns='Port', values='Count')
print (df1)
Port 80 443 3128
StartDate
2011-08-10 11:07:10 NaN NaN 10.0
2011-08-10 11:07:40 1.0 1.0 10.0
2011-08-10 11:08:00 NaN 1.0 9.0
2011-08-10 11:08:20 1.0 NaN NaN
df1.plot.bar()
Upvotes: 1