본문 바로가기

Python

Python 활용 #2 - project: 3일 별로 번갈아가는 일정을 코딩하자.

Project 목표: 3일 별로 번갈아가는 일정을 코딩하자.

- 사용 언어: python

- 사용 package: pandas

- 사용 개념: mod, dataframe, range, if.. for, int 등 

- 작성일: 2021년 4월 12일 

 

 

목차

1. 결과물

2. Motivation

3. 주어진 정보

4. 코딩 설명 + 구조 (영어) 

5. 코딩 전문

 

1. 결과물

최종 결과물: 3영업일 office, 그 다음 3 영업일 home (가장 오른쪽 열(column))

2. Motivation

1) 최근 재택근무(work from home)와 사무실 출근을 병행하고 있다.

2) 재택근무 3 영업일 이후 사무실 출근 3 영업일, 다시 재택근무 3 영업일 이렇게 돌아가며 일하고 있다.

3) 코딩을 통해 재택근무-사무실 출근 일정을 작성해 보기로 했다. 

 

 

3. 주어진 정보

1) 시작 날짜 = 2021년 4월 12일 

2) 시작 날짜의 일정 = office (사무실 출근을 의미)

3) 일정 형태 = 사무실 출근 3 영업일 + 이후 재택근무 3 영업일 

4) 주말(weekends)에는 일하지 않는다 -> 사무실 출근을 하지도 않고, 재택근무를 하지도 않는다. 

5) 코딩 대상 영업일 숫자 = 50 영업일 

6) 시작 날짜 ~ 코딩 대상 영업일 동안 휴일: 2021년 5월 1일 근로자의 날, 5월 5일 어린이날, 5월 19일 부처님 오신날 -> 주말과 마찬가지로, 사무실 출근을 하지도 않고, 재택근무를 하지도 않는다. 

 

 

4. 코딩 설명 + 구조 (영어) 

# create a dataframe called 'df' with column names Dates, Weekday, business_day_count, mod_ work_from home
# the final end-picture: function(Dates) -> work_from_home 
# the "mathemtical range" of the function(Dates) = {office, home}
# example function('2021-04-13') = 'office' 

# weekday indicates the day of the week. Weekdays = {Monday, Tuesday, Wednesday, ... , Sunday}
# in python, weekday is counted numerically which starts with 0, 1, ... 6. Monday = 0, Tuesday = 1, ... Sunday = 6

# business_day_count is the number of business days from a specific date, which is the'start date' in this code.
# mod indicates the remainder. example: 7 mod 3 = 1. That is, 7/3 = (quotient = 2, remainder =1) 

# construction of the output "work_form_office" using the concept 'mod' 
# in this project, business_day_count mod n will be computed to divide the business days into categories.
# for example, suppose 8 days alternate (= work for 4 days in office + work for 4 days at home) 
# remainders in the set {0,1,2,3} would be directed to "office" 
# remainders in the set {4,5,6,7} would be directed to "home"
# that is, the business days = {0,1,2,3,4,5,6,7,8,9,10,11, .... } would be classfied to two sets {0,1,2,3} and {4,5,6,7}
# for n in the set "business days" = {0, 1,2...}  -> "mods" = {{0,1,2,3}, {4,5,6,7}} -> "work from home" =  {office, home}

 

 

5. 코딩 전문

# import python library 'pandas' 
# pandas provides the 'data frame' concept-related supports 
import pandas as pd

# length indicates the number of calendar days 
length = 50

df = pd.DataFrame({'Dates': range(length),
                   'Weekday': range(length), 
                   'business_day_count': range(length),
                   'mod': range(length),
                   'work_from_home': range(length)
                   })

# import 
import datetime
from datetime import date, timedelta


# 2021년 4월~5월간의 공휴일을 입력한다. 아래에 첫 시작일이 2021년 4월 12일 ~ 이후 50일 대상 
holiday = pd.DataFrame({'Dates': [datetime.date(2021, 5, 1), 
                                  datetime.date(2021,5,5), 
                                  datetime.date(2021,5,19)],
                        'holiday_name': ['근로자의 날', 
                                         '어린이날', 
                                         '부처님 오신날']
                        })
                        
# set the initial value
df['business_day_count'][0] = 0
startdate = date(2021, 4, 12)

for i in range(len(df)):
    df['Dates'][i] = startdate + datetime.timedelta(days=i)
    df['Weekday'][i] = df['Dates'][i].weekday()
    if df['Weekday'][i] == 6 or df['Weekday'][i] == 5:
        df['business_day_count'][i] = "Weekends"
    for j in range(len(holiday)):
        if holiday['Dates'][j] == df['Dates'][i]:
            df['business_day_count'][i] = "Weekends"
            
t=6
t=int(t)

j = 0
for i in range(0,len(df)):
    if df['business_day_count'][i] == "Weekends":
        df['mod'][i] = "Weekends"
        df['work_from_home'][i] = "Weekends"
    else: 
        df['business_day_count'][i] = j
        df['mod'][i] = df['business_day_count'][i] % t
        j = j+1
        if df['mod'][i] in range(0,int(t/2)):
            df['work_from_home'][i] = "office"
        elif df['mod'][i] in range(int(t/2),t):
            df['work_from_home'][i] = "home"
        else: print("Error. Should not reach here.")
 
 df

 

 

Project 목표: 3일 별로 번갈아가는 일정을 코딩해 보자.

- 사용 언어: python

- 사용 package: pandas

- 사용 개념: mod, dataframe, range, if.. for, int 등 

- 작성일: 2021년 4월 12일