Home > Back-end >  Using Pandas to filter a row from a column and using sum() from a DataFrame
Using Pandas to filter a row from a column and using sum() from a DataFrame

Time:06-08

My df contains the columns MCC Description (which contains categories), Amount (in dollars) and Calendar Year. Advertising services is a row within MCC Description.

I am trying to calculate the total amount spent for per year at advertising services.

df.groupby('MCC Description')['Amount'].sum()

Which gives the following output

MCC Description
ACCOUNTING AUDITING AND BOOKKEEPING SER         89.50
ADVERTISING SERVICES                         91833.27
AIR CONDITIONING AND REFRIGERATION REPAI       202.00
ALL OTHER DIRECT MARKETERS                  238780.52
AMUSEMENT RECREATION SERVICES (SWIMMING       5731.80
                                              ...    
VOCATIONAL AND TRADE SCHOOLS                  3786.20
WELDING                                        910.31
WINDOW COVERING UPHOLSTERY AND DRAPERY         325.00
WOMEN S READY TO WEAR STORES                   300.00
WRECKING AND SALVAGE YARDS                    4247.98
Name: Amount, Length: 183, dtype: float64

I also tried this variation, but also not right

df[(df['MCC Description'] == 'Advertising services')][['Amount', 'Calendar Year']]

Which gives this output (with no values)

Amount  Calendar Year

CodePudding user response:

df[df['MCC Description'].eq('ADVERTISING SERVICES')].groupby('Calendar Year')['Amount'].sum()
  • Related