I have a DataFrame quantities
as follows:
JavaScript
x
6
1
> quantities.head(2).to_dict()
2
3
{'created': {0: Timestamp('2022-08-09 00:02:33.786000'),
4
1: Timestamp('2022-08-09 00:02:33.787000')},
5
'quantity': {0: 1, 1: 1}}
6
How do I make a histogram out of it with date along x axis, total quantity for the date on y axis?
I tried
JavaScript
1
2
1
quantities = quantities.set_index("created")
2
But the resulting histogram didn’t make any sense to me.
Edit. Long day. I got it all wrong. Per comments, it looks like I what I need is a bar chart, or even a regular line plot, with date along the x axis, and the total sum of quantities for the given day along y. I need to find a way to group by day. Help?
Advertisement
Answer
Group by day:
JavaScript
1
14
14
1
import pandas as pd
2
3
df = pd.DataFrame({'created': {0: '2022-08-09 00:02:33.786000',
4
1: '2022-08-10 00:02:33.787000',
5
2: '2022-08-11 00:03:33.787000',
6
3: '2022-08-10 00:04:33.787000'},
7
'quantity': {0: 1,
8
1: 1,
9
2: 1,
10
3: 2}})
11
12
df['created'] = pd.to_datetime(df['created']) # probably not necessary for you if created is already a datetime
13
df.groupby(df['created'].dt.floor('d')).sum()
14