Avoid KeyError while selecting several data from several groups

Question:

I am trying to add certain values from certain Brand from certain Month by using .groupby, but I keep getting the same Error: KeyError: (‘Acura’, ‘1’, ‘2020’)

This Values Do exist in the file i am importing:

ANIO    ID_MES  MARCA   MODELO  UNI_VEH
2020    1   Acura   ILX 6
2020    1   Acura   Mdx 19
2020    1   Acura   Rdx 78
2020    1   Acura   TLX 7
2020    1   Honda   Accord- 195
2020    1   Honda   BR-V    557
2020    1   Honda   Civic   693
2020    1   Honda   CR-V    2095

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_excel("HondaAcuraSales.xlsx")


def sumMonthValues (year, brand):
    count = 1
    sMonthSum = []
    if anio == 2022:
        months = 10
    else:
        months = 12
    while count <= months:
        month = 1
        monthS = str(mes)
        BmY = df.groupby(["BRAND","ID_MONTH","YEAR"])
        honda = BmY.get_group((brand, monthS, year))
        sales = honda["UNI_SOL"].sum()
        sMonthSum += [sales]
        month = month + 1
    return sumasMes


year = 2020
brand = ('Acura')

chuck = sumMonthValues (year, brand)

print (chuck)

Is there something wrong regarding how am i grouping the data?

Answers:

If need filter DataFrame by year, brand and months you can avoid groupby and use DataFrame.loc with mask – if scalar compare by Series.eq, if multiple values use Series.isin:

def sumMonthValues (year, brand):
    
    months = 10 if year == 2022 else 12
    
    mask = (df['ID_MES'].isin(range(1, months+1)) &
            df['ANIO'].eq(year) & 
            df['MARCA'].isin(list(brand)))
    
    return df.loc[mask, "UNI_VEH"].sum()

year = 2020
#one element tuple - added ,
brand = ('Acura', )

chuck = sumMonthValues (year, brand)
print (chuck)
110
Answered By: jezrael

So i got arround it: Storing the values given from the sum of sales given year and brand per month.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_excel("ventasHondaMexico2020-2019.xlsx")

def sumMonthValues (year, brand):
    
    sMonthSum = []
    months = 10 if year == 2022 else 12
    nmes = 1
    mes = [nmes]
    while nmes <= months:
        mask = (df['ID_MES'].isin(mes) &
                df['ANIO'].eq(year) & 
                df['MARCA'].isin(list(brand)))
        nmes = nmes +1
        mes = [nmes]
        sumMes = df.loc[mask, "UNI_VEH"].sum()
        sMonthSum += [sumMes]
    return sMonthSum
        

year = 2020
#one element tuple - added ,
brand = ('Acura', )
conteo = 1 
chuck = sumMonthValues (year, brand)
print (chuck)
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.