本文共 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/