Python Pandas 按邮政编码堆叠并按 month/year 分组

Python Pandas stack by zip code and group by month/year

我有一个包含交易数据的大型数据框。我想做的是使用 python 聚合数据,从邮政编码开始,然后是年和月,最后是当月的交易总数。

我的DF:

  Date        VAR1   VAR2    ZipCode    Transactions
YYYY-MM-DD.    X.     Y.     12345.         1.      

所以我做的第一件事就是转换日期时间

 df['Date'] = pd.to_datetime(df['Date'])
 df.info()
 # Date datetime64[ns]

然后我将数据拆分为年月和交易数量:

# grouping the data by year and month
per = df.Date.dt.to_period("M")  
g = df.groupby(per)
g.sum() # so now that this works, we need to break it up into zip codes

输出为:

Date.       Transactions
YYYY-MM.        X
YYYY-MM.        Y

我的问题是,我缺少什么才能获得前面的邮政编码:

ZipCode.     Date.    Transactions
 123345.   YYYY-MM.     sum()

非常感谢任何帮助

如果需要按 zip 和按月分组,我认为您需要将列 ZipCode 添加到 groupby

per = df.Date.dt.to_period("M")
df1 = df.groupby(['ZipCode',per])['Transactions'].sum().reset_index()