博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python实现判断某天是否是节假日
阅读量:4291 次
发布时间:2019-05-27

本文共 906 字,大约阅读时间需要 3 分钟。

import pandas as pdfrom datetime import datetimeimport pandas as pdstartdate = '2016-01-01'enddate = '2016-12-31'holiday = ['01-01', '05-01']datalist = list(pd.date_range(start=startdate, end=enddate))df = pd.DataFrame({'date':datalist})print("df:\n",df)df['month'] = df['date'].apply(lambda x: x.month)df['day'] = df['date'].apply(lambda x: x.day)df['weekday'] = df['date'].apply(lambda x: x.weekday()+1)print("df:\n",df)#工作日与休息日isrest = ((df['weekday'] == 6) | (df['weekday'] == 7))print("isrest:\n",isrest)df['label'] = isrest*1 #布尔值转数值print("df['label']:\n",df['label'])print("df:\n",df)#节假日for h in holiday:    #h = '01-01'    h_month,h_day = h.split('-')    print("h_month,h_day:\n",h_month,h_day)    h_index = ((df['month'] == int(h_month)) & (df['day'] == int(h_day)))    print("h_index:\n",h_index)    df.loc[h_index, 'label'] = 2print("df:\n",df)

参考:https://bbs.pinggu.org/thread-5980288-1-1.html

转载地址:http://chhgi.baihongyu.com/

你可能感兴趣的文章
MySQL索引实战汇总
查看>>
使用ssh在远程linux服务器上安装oracle
查看>>
spring的xml中注册bean的时候报错1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是 <xsd:schema>
查看>>
连接Linux服务器操作Oracle数据库
查看>>
mongodb 误删除集合恢复 误删除表数据恢复
查看>>
整理项目改成https访问的操作手册
查看>>
架构必备词汇整理
查看>>
java常见的集合及其关系
查看>>
成为出色的程序员必修之路-数据结构(总结)
查看>>
今天被一个架构师面了
查看>>
java学习建议
查看>>
Java传参方式
查看>>
分布式补偿事务处理方案 / 分布式计算是如何控制事务的?
查看>>
分布式定时任务——elastic-job
查看>>
Spring中配置数据源的4种形式(含有如何在spring框架中解决多数据源的问题)
查看>>
分布式与集群有什么区别?
查看>>
linux安全-禁止密码登录及root登录
查看>>
Java 中的类为什么要实现序列化呢 / JAVA中序列化和反序列化中的静态成员问题
查看>>
redis集群搭建及注意事项
查看>>
分布式演变过程中之Session集群解决方案
查看>>