【发布时间】:2020-05-21 21:49:40
【问题描述】:
与这个问题类似,但增加了一个步骤:Rolling groupby nunique count based on start and end dates
我有一个具有唯一 ID、开始日期、结束日期、开始年份和结束年份的数据框。在这段时间内,ID 可以启动、停止和重新启动。
我想在一年中获得一个 groupby nunique 的 ID 计数。目前,我可以计算 ID 的开始日期和结束日期的唯一值,但我如何准确地合并包括年份?
fun = pd.DataFrame({'ZIP_KEY': ['A', 'B', 'A'],
'start_month': [1, 2, 2],
'end_month': [4, 3, 7],
'start_year': [2016, 2016, 2017],
'end_year': [2016, 2017, 2018]})
fun["month_list"] = fun.apply(lambda x: list(range(x["start_month"], x["end_month"]+1)), axis=1)
fun["year_list"] = fun.apply(lambda x: list(range(x["start_year"], x["end_year"]+1)), axis=1)
fun = fun.explode("month_list")
fun = fun.explode("year_list")
fun.groupby(["year_list", "month_list"])["ZIP_KEY"].nunique()
year_list month_list
2016 1 1
2 2
3 2
4 1
2017 2 2
3 2
4 1
5 1
6 1
7 1
2018 2 1
3 1
4 1
5 1
6 1
7 1
如果 Zip Key 是多年的,我目前的方法没有考虑全年 -->
从 2018 年 1 月开始,到 2020 年 2 月结束,然后我们得到 [1,2] 和 [2018,2019,2020],而不是 2018 年和 2019 年的完整年份。我应该得到计数 [1,2,3,4,5,6,7,8,9,10,11,12] 的 [2018, 2019] 和 [1,2] 的 2020 年
【问题讨论】: