鳄梨数据框的循环

时间:2019-04-04 09:56:52

标签: python pandas loops dataframe indexing

我的for循环中不断出现以下错误:

KeyError: "['California Total Sales'] not in index"

我不确定为什么以上内容不在我的索引中?

df101 = pd.DataFrame

for region in df['region'].unique()[:5]:
    print(region)

    regionalsales_df = df.copy()[df['region']==region]
    #regionalsales_df.set_index('Date')
    regionalsales_df.sort_index(inplace=True)

    regionalsales_df[f"{region}Total Sales"] = regionalsales_df['AveragePrice']*regionalsales_df['Total Volume']

    if df101.empty:
        df101 = regionalsales_df[[f"{region} Total Sales"]]

    else:
        df101 = df101.join(regionalsales_df[f"{region} Total Sales"])

df101

我正在尝试获取一个数据框,其中的索引是日期,而列是该地区的总销售额

1 个答案:

答案 0 :(得分:0)

也许您可以df.groupby(“ region”)。sum()