为了账号安全,请及时绑定邮箱和手机立即绑定

在熊猫中获取一个月级别的两个日期时间之间的天数

在熊猫中获取一个月级别的两个日期时间之间的天数

慕姐8265434 2023-06-13 15:10:33
这是一个数据集,其中包含人员被分配到某个角色的时间,并具有他们的开始日期,以及他们担任角色的月份的年月:  | ID | Name | strt_dt | end_dt | yearmo |   | 1  | Jay  | 4-22-19 | 7-30-19| 201904 |   | 1  | Jay  | 4-22-19 | 7-30-19| 201905 |    | 1  | Jay  | 4-22-19 | 7-30-19| 201906 |     | 1  | Jay  | 4-22-19 | 7-30-19| 201907 |    | 2  | Fao  | 7-14-19 |10-14-19| 201907 |      | 2  | Fao  | 7-14-19 |10-14-19| 201908 |     | 2  | Fao  | 7-14-19 |10-14-19| 201909 |     | 2  | Fao  | 7-14-19 |10-14-19| 201910 |    我要计算这个人在这个角色中的每一年,那个月有多少天他们在这个角色中。输出应如下所示:  | ID | Name | strt_dt | end_dt | yearmo | no_of days|  | 1  | Jay  | 4-22-19 | 7-30-19| 201904 |  9 |  | 1  | Jay  | 4-22-19 | 7-30-19| 201905 |  31|    | 1  | Jay  | 4-22-19 | 7-30-19| 201906 |  30|    | 1  | Jay  | 4-22-19 | 7-30-19| 201907 |  30|   | 2  | Fao  | 7-14-19 |10-14-19| 201907 |  18|    | 2  | Fao  | 7-14-19 |10-14-19| 201908 |  31|    | 2  | Fao  | 7-14-19 |10-14-19| 201909 |  30|    | 2  | Fao  | 7-14-19 |10-14-19| 201910 |  14|  我试图从 strt 中提取它们的日期(将其减去 30 以获得 ddays 的编号)和结束日期并创建一个单独的列。但我坚持如何从那里开始。欢迎任何想法或建议。df['strt_yearmo'] = df['strt_dt'].dt.year * 100 +df['strt_dt'].dt.monthdf['end_yearmo'] = df['end_dt'].dt.year * 100 +df['end_dt'].dt.month  | ID | Name | strt_dt | end_dt | yearmo | strt_yearmo|end_yearmo|  | 1  | Jay  | 4-22-19 | 7-30-19| 201904 |  201904    |201907|  | 1  | Jay  | 4-22-19 | 7-30-19| 201905 |  201904    |201907|  | 1  | Jay  | 4-22-19 | 7-30-19| 201906 |  201904    |201907|    | 1  | Jay  | 4-22-19 | 7-30-19| 201907 |  201904    |201907 |  | 2  | Fao  | 7-14-19 |10-14-19| 201907 |  201907    |201910 |  | 2  | Fao  | 7-14-19 |10-14-19| 201908 |  201907    |201910 |   | 2  | Fao  | 7-14-19 |10-14-19| 201909 |  201907    |201910 |  | 2  | Fao  | 7-14-19 |10-14-19| 201910 |  201907    |201910 | 
查看完整描述

1 回答

?
千巷猫影

TA贡献1829条经验 获得超7个赞

np.select(condition, choice,alternative) 在将日期强制转换为日期时间并在中提取结束月份日期后使用yearmo


从 yearmo 中提取月末日期


df['startmo']=pd.to_datetime(df['yearmo'].astype(str), format='%Y%m')+ pd.offsets.MonthEnd(0)

胁迫strt_dt和end_dt约会


datedf['strt_dt'],df['end_dt']=pd.to_datetime(df['strt_dt']),pd.to_datetime(df['end_dt'])


提出条件


conditions=[df.startmo.dt.month==df.strt_dt.dt.month, df.startmo.dt.month==df.end_dt.dt.month]


#If month in yearmo is the same with strt_dt,substract strt_dt from endmont.

#If month in yearmo is the same with end_dt, extract the days in end_dt

提出对应于上述每个条件的选择


choices=[df.startmo.sub(df.strt_dt).dt.days+1,df.end_dt.dt.day]

通过匹配条件和选择来计算天数。也包括备选方案。这里的替代方案是开始和结束的月份与 yearmo 不匹配,这意味着月份在中间所以只需提取日期作为条件的替代方案


df['no_of days']=np.select(conditions,choices,df.startmo.dt.day)





ID Name    strt_dt     end_dt  yearmo    startmo  no_of days

0   1  Jay 2019-04-22 2019-07-30  201904 2019-04-30           9

1   1  Jay 2019-04-22 2019-07-30  201905 2019-05-31          31

2   1  Jay 2019-04-22 2019-07-30  201906 2019-06-30          30

3   1  Jay 2019-04-22 2019-07-30  201907 2019-07-31          30

4   2  Fao 2019-07-14 2019-10-14  201907 2019-07-31          18

5   2  Fao 2019-07-14 2019-10-14  201908 2019-08-31          31

6   2  Fao 2019-07-14 2019-10-14  201909 2019-09-30          30

7   2  Fao 2019-07-14 2019-10-14  201910 2019-10-31          14


查看完整回答
反对 回复 2023-06-13
  • 1 回答
  • 0 关注
  • 86 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信