023_透视表_分组_聚合
import pandas as pd import numpy as np if __name__ == '__main__': pd.options.display.max_columns = 999 orders = pd.read_excel("C:/Users/18124/Desktop/pandas/023_透视表_分组_聚合/Orders.xlsx") print(orders.head()) # 显示列类型 - 日期 print(orders.Date.dtype) # 查看类型 # 添加新列 - 年份 orders["Year"] = pd.DatetimeIndex(orders["Date"]).year print(orders.head()) # 方法一 : 透视表 pt1 = orders.pivot_table(index="Category", columns="Year", values="Total", aggfunc=np.sum) print(pt1) # 方法二 : groups = orders.groupby(["Category", "Year"]) s = groups["Total"].sum() c = groups["ID"].count() pt2 = pd.DataFrame({"Sum":s, "Count":c}) print(pt2)