I am having difficulty creating a stacked bar chart time series from my Pandas dataframe (image below). I would like to have the 'Date' on the x axis, the 'Hours' on the y axis, and each bar to show the time spent with each group in 'Category'.
Do I need to use Pandas - Groupby function? The dataframe is a sample. I have hundreds of rows of data from 2018 to 2020.
pandas.DataFrame.groupby
on'date'
and'group'
, while aggregating.sum
on'time'
.dt
extractor is used to extract only the.date
component of the'date'
column.'Date'
column of your dataframe is properly formatted as adatetime
dtype
, withdf.Date = pd.to_datetime(df.Date)
dfg
, must be shaped into the correct form, which can be accomplished withpandas.DataFrame.pivot
.pandas.DataFrame.plot.bar
and use thestacked
parameter.pandas.DataFrame.plot
for all the parameters.Imports and Data Transformation
.pivot_table
, which both reshapes and aggregatesindex=df.date.dt.date
is used so the index doesn't include the time component, since the data for the entire day is being aggregated.Plot
pandas.DataFrame.barh