【发布时间】:2020-08-31 19:56:48
【问题描述】:
假设我有以下数据框:
df = pd.DataFrame({'c': ['abc', 'def', 'wyx', 'abc', 'wyx'], 'begin_date': ['2020-01-01', '2000-12-23', '2003-07-07', '2005-03-02', '2004-01-01'], 'end_date': ['2020-01-31', '2001-02-02', '2004-03-02', '2005-04-01', '2004-07-04']})
df
c begin_date end_date
abc 2020-01-01 2020-01-31
def 2000-12-23 2001-02-02
wyx 2003-07-07 2004-03-02
abc 2005-03-02 2005-04-01
wyx 2004-01-01 2004-07-04
我想按 c 分组并计算该 c 的一行在 begin_date 和 end_date 之间的唯一天数,我的最终数据框将是:
end = pd.DataFrame({'c': ['abc', 'def', 'wyx'], 'count': [30, 41, 363]})
end
c count
abc 30
def 41
wyx 363
【问题讨论】:
标签: python pandas dataframe datetime