0

I have a pandas dataframe looking like that:

         Datum     Wann  counter
0   2022-11-14   abends       50
1   2022-11-14  morgens        1
2   2022-11-14  morgens        2
3   2022-11-14  morgens        4
4   2022-11-14  morgens        2
5   2022-11-14  morgens        1
6   2022-11-14  morgens        1
7   2022-11-14   abends        1
8   2022-11-14   abends        2
9   2022-11-14   abends        1
10  2022-11-14   abends        1
11  2022-11-14   abends        1
12  2022-11-14   abends        3
13  2022-11-16   abends       10
14  2022-11-16  morgens        1
15  2022-11-16  morgens        4
16  2022-11-17   abends        6
17  2022-11-27   abends       10
18  2022-11-29  morgens        5
19  2022-11-29   abends        2
...

and would like to plot a stacked bar chart where the x axis is the date (just one bar per date) and the sum of counter per time is stacked per time leading to a plot

meaning 2022-11-14 abends 50 morgens 11 one bar
        2022-11-16 abends 10 morgens 1 one bar and so on

I coded:

df = pd.DataFrame(datasheet.get_all_records())
pct_list = df[["Datum", "Wann", "counter"]]#    pct_list.reset_index(inplace=True)
pct_list.groupby(["Datum", "Wann"])["counter"].sum().reset_index()
pct_list = pct_list.pivot_table(index="Datum", values="counter", columns="Wann") \
    .reset_index() \
    .rename_axis(None, axis=1)
pct_list.plot.bar(x="Datum", stacked=True)

and receive as input for the plot with totaly strange sums Datum abends morgens 0 2022-11-14 8.428571 1.833333 1 2022-11-16 10.000000 2.500000 ...

leading to a wrong chart chart

mozway
  • 194,879
  • 13
  • 39
  • 75
Jochen
  • 1
  • Remove `pct_list.groupby(["Datum", "Wann"])["counter"].sum().reset_index()` and use `pct_list = pct_list.pivot_table(index="Datum", values="counter", columns="Wann", aggfunc='sum')` – jezrael Jan 31 '23 at 07:03
  • 1
    Wow, many thanks! kudos to mozway for reformatting (sorry my first post) and jezrael for solving it! – Jochen Jan 31 '23 at 07:12

0 Answers0