I have an Excel file as below
I want to plot the output by groupby Name and Fruit on the left hand side and top groupby date as shown as below.
I tried create the Excel file like this:
Output = dt.groupby(['Name','Fruits'])['QTY'].sum()
but I do not know how does the QTY can split by the Date on top of it.
Advertisement
Answer
UPDATE
If your indexes are duplicated, use pivot_table()
instead and use an aggregation function like this (see the note at the end of this section):
df.pivot_table(index=['Name', 'Fruits'], columns=['Date'], values='QTY', aggfunc='sum', fill_value=0)
That gives me the following results:
ORIGINAL
If using Pandas, you can pivot the DataFrame as such:
df.pivot(index=['Name', 'Fruits'], columns=['Date'], values='QTY').fillna(0)
The fillna() call places zeroes where the table has NaNs. If you don’t mind that, you might as well just remove that function call.