Home > OS >  How do I plot a histogram with one of the columns along the x axis?
How do I plot a histogram with one of the columns along the x axis?

Time:09-22

I have a DataFrame quantities as follows: enter image description here

> quantities.head(2).to_dict()

{'created': {0: Timestamp('2022-08-09 00:02:33.786000'),
  1: Timestamp('2022-08-09 00:02:33.787000')},
 'quantity': {0: 1, 1: 1}}

How do I make a histogram out of it with date along x axis, total quantity for the date on y axis?

I tried

quantities = quantities.set_index("created")

But the resulting histogram didn't make any sense to me.

enter image description here

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?

CodePudding user response:

Group by day:

import pandas as pd

df = pd.DataFrame({'created': {0: '2022-08-09 00:02:33.786000',
                               1: '2022-08-10 00:02:33.787000',
                               2: '2022-08-11 00:03:33.787000',
                               3: '2022-08-10 00:04:33.787000'},
                   'quantity': {0: 1, 
                                1: 1,
                                2: 1,
                                3: 2}})

df['created'] = pd.to_datetime(df['created'])  # probably not necessary for you if created is already a datetime
df.groupby(df['created'].dt.floor('d')).sum()
  • Related