Home > front end >  Pandas: groupby multiple columns for bar graph
Pandas: groupby multiple columns for bar graph

Time:01-29

I have a CSV of financial data that is listed by:

  1. Date
  2. Category
  3. Amount

The dataset looks like the following, with another hundred rows. I am trying to use pandas to graph this data by month and then the total by each category. An example would be a bar graph of each month (Jan - Dec) with a bar for the total of each Category type (Food & Drink, Shopping, Gas).

    Date                Category                Amount
0   12/29/2022          Food & Drink            -28.35  
1   12/30/2022          Shopping                -12.12  
2   11/30/2022          Food & Drink            -12.30  
3   11/30/2022          gas                     -12.31  
4   10/30/2022          Food & Drink            -6.98
....

My initial code worked, but totaled everything in the month and didn't separate by category type.

df['Transaction Date'] = pd.to_datetime(df['Transaction Date'], format='%m/%d/%Y') df = df.groupby(df['Transaction Date'].dt.month)['Amount'].sum()

enter image description here

My next try to separate the monthly information out by Category failed.

df = df.groupby((df['Transaction Date'].dt.month),['Category'])['Amount'].sum()

How can I graph out each month by the sum of Category type?

Here is an example. enter image description here

CodePudding user response:

You can create a new column for months within the data frame and then groupby months and category columns

df['Months'] = df['Transaction Date'].dt.month_name()
df = df.groupby(['Months', 'Category']).agg({'Amount':'sum'})
  • Related