I have this dataframe
JavaScript
x
11
11
1
df=pd.DataFrame([["2017-01-14",1],
2
["2017-01-14",30],
3
["2017-01-16",216],
4
["2017-02-17",23],
5
["2017-02-17",2],
6
["2017-03-19",745],
7
["2017-03-19",32],
8
["2017-03-20",11],
9
["2017-03-20",222],
10
["2017-03-21",4]],columns=["date","payout_value"])
11
To aggregate payout_value
by date I use:
JavaScript
1
12
12
1
df_daily=df.groupby('date').agg(['sum'])
2
3
payout_value
4
sum
5
date
6
2017-01-14 31
7
2017-01-16 216
8
2017-02-17 25
9
2017-03-19 777
10
2017-03-20 233
11
2017-03-21 4
12
How do I plot (bar chart) dates on x-axis and aggregated payout sum on y axis?
I tried using df.plot(x='date', y='payout_value',kind="bar")
approach, but there is no ‘date’ column in df_daily
dataframe, print(list(df_daily))
gives [('payout_value', 'sum')]
Advertisement
Answer
you are almost there,
use reset_index
and plot your by df_daily
JavaScript
1
4
1
df_daily=df.groupby('date').agg(['sum']).reset_index()
2
df_daily.plot(x='date', y='payout_value',kind="bar")
3
plt.show()
4