首页 > 解决方案 > 使用 pandas groupby 计算列

问题描述

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

excel_file = "dataset.xlsx"

sheet0 = pd.read_excel(excel_file, 'Title Sheet')
sheet3 = pd.read_excel(excel_file, sheet_name='CustomerDemographic')
sheet4 = pd.read_excel(excel_file, sheet_name='CustomerAddress')
sheet1 = pd.read_excel(excel_file, sheet_name='Transactions')

customer_data = pd.concat([sheet3, sheet4, sheet1])

#TODO: Data calculations
pivot = sheet3.groupby(['customer_id']).mean()
bestCustomers = pivot.loc[:,"past_3_years_bike_related_purchases":"tenure"]

但它给出了 KeyError: 'customer_id' ,问题到底出在哪里?

标签: pythonpandaspandas-groupby

解决方案


推荐阅读