我试图通过使用.groupby从某个月添加某些品牌的某些值,但我仍然得到相同的错误: KeyError:('Acura','1','2020')
这个值确实存在于我要导入的文件中:
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)
我是如何分组数据的,有什么问题吗?
发布于 2022-12-02 06:35:58
如果需要通过year, brand
和月份筛选year, brand
,则可以避免使用DataFrame.loc
和掩码-如果标量通过Series.eq
进行比较,如果多个值使用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
发布于 2022-12-02 17:21:25
因此,我找到了它:存储从给定的年销售额和品牌每月之和给出的价值。
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)
https://stackoverflow.com/questions/74651550
复制相似问题