포스팅 목차
126. Display those employees who joined in the company in the month of DEC?
* 12월에 입사원 사원들의 정보를 출력하시오.
- 파이썬 & R 패키지 호출 및 예제 데이터 생성 링크
- 유사문제 : 22/66 / 67 / 87 / 125번
- 텍스트 형태의 경우 substr 함수를 사용하여서 월부분을 추출하여 처리 가능
- [데이터 추출] 특정 날짜에 해당하는 데이터 추출 - 영문월(Month) 처리, 날자형 데이터 날짜형 문자로 변환
- 데이터 전처리 - SQL, Pandas, R Prog, Dplyr, Dfply, SQLDF, PANDASQL, DATA.TABLE, SAS, Proc Sql
|
1. Oracle(오라클)
입사일에서 월(Month) 부분을 추출하여서 12월에 입사한 직원들의 정보를 출력한다.
Oracle Programming |
select *
from emp
where upper(to_char(hiredate,'mon')) = 'DEC';
2. Python Pandas(파이썬)
to_datetime함수를 사용하여서 입사일자에서 월(Month)에 해당하는 full name을 추출 후 앞에서 3글자를 검색하여서 12월(‘DEC’)에 입사한 직원들의 정보를 출력한다.
Python Programming |
emp[ pd.to_datetime(pd.to_datetime(emp['hiredate']), format='%m').dt.month_name().str.slice(stop=3).str.upper() == 'DEC' ]
Results |
empno | ename | job | mgr | hiredate | sal | comm | deptno | |
0 | 7369 | SMITH | CLERK | 7902.0 | 1980/12/17 | 800 | NaN | 20 |
7 | 7788 | SCOTT | ANALYST | 7566.0 | 1982/12/09 | 3000 | NaN | 20 |
11 | 7900 | JAMES | CLERK | 7698.0 | 1981/12/03 | 950 | NaN | 30 |
12 | 7902 | FORD | ANALYST | 7566.0 | 1981/12/03 | 3000 | NaN | 20 |
3. R Programming (R Package)
Month 함수를 사용하여서 입사일자에서 월에 해당하는 정보를 추출 후 12월(‘DEC’)에 입사한 직월들의 정보를 출력한다.
R Programming |
%%R
emp[ toupper(month.abb[month(as.Date(emp$hiredate))]) == 'DEC' ,]
Results |
# A tibble: 4 x 8
empno ename job mgr hiredate sal comm deptno
<dbl> <chr> <chr> <dbl> <date> <dbl> <dbl> <dbl>
1 7369 SMITH CLERK 7902 1980-12-17 800 NA 20
2 7788 SCOTT ANALYST 7566 1982-12-09 3000 NA 20
3 7900 JAMES CLERK 7698 1981-12-03 950 NA 30
4 7902 FORD ANALYST 7566 1981-12-03 3000 NA 20
- [참고] 해당 월의 영문 약자를 검색
R Programming |
%%R
month.abb
Results |
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
- [참고] 입사일자(hiredate)에서 월을 추출하여서 timestamp 변수로 생성한다.
R Programming |
%%R
transform(emp, timestamp = format(substr(hiredate, 6, 7))) %>% head()
Results |
empno ename job mgr hiredate sal comm deptno timestamp
1 7369 SMITH CLERK 7902 1980-12-17 800 NA 20 12
2 7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30 02
3 7521 WARD SALESMAN 7698 1981-02-22 1250 500 30 02
4 7566 JONES MANAGER 7839 1981-04-02 2975 NA 20 04
5 7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30 09
6 7698 BLAKE MANAGER 7839 1981-03-01 2850 NA 30 03
4. R Dplyr Package
Month 함수를 사용하여서 입사일자에서 월에 해당하는 정보를 추출 후 12월(‘DEC’)에 입사한 직월들의 정보를 출력한다.
R Programming |
%%R
emp %>% filter( toupper(month.abb[month(as.Date(emp$hiredate))]) == 'DEC' )
Results |
# A tibble: 4 x 8
empno ename job mgr hiredate sal comm deptno
<dbl> <chr> <chr> <dbl> <date> <dbl> <dbl> <dbl>
1 7369 SMITH CLERK 7902 1980-12-17 800 NA 20
2 7788 SCOTT ANALYST 7566 1982-12-09 3000 NA 20
3 7900 JAMES CLERK 7698 1981-12-03 950 NA 30
4 7902 FORD ANALYST 7566 1981-12-03 3000 NA 20
5. R sqldf Package
strftime 함수를 사용하여서 입사일자에서 월에 해당하는 형식(format)으로 변환하여서 12월에 입사한 직원의 정보를 출력한다.
- 현재 hiredate가 “real” 형태로 저장되어 있음. / sqlite에는 날짜형이 존재 안 함 (129번 sqldf)
R Programming |
%%R
sqldf("select strftime('%m', hiredate * 3600 * 24, 'unixepoch') hire_month,
a.*
from emp
where strftime('%m', hiredate * 3600 * 24, 'unixepoch') = '12'")
Results |
hire_month empno ename job mgr hiredate sal comm deptno
1 12 7369 SMITH CLERK 7902 1980-12-17 800 NA 20
2 12 7788 SCOTT ANALYST 7566 1982-12-09 3000 NA 20
3 12 7900 JAMES CLERK 7698 1981-12-03 950 NA 30
4 12 7902 FORD ANALYST 7566 1981-12-03 3000 NA 20
6. Python pandasql Package
hiredate가 텍스트 형태로 입력되어 있어서 날짜형으로 변환 후 월에 해당하는 형식(format)으로 변환하여서 12월에 입사한 직원의 정보를 출력한다.
Python Programming |
ps.sqldf(" select strftime('%m',(substr(hiredate,1,4)||'-'||substr(hiredate,6,2)||'-'||substr(hiredate,9,2))) hire_month, * \
from emp \
where strftime('%m',(substr(hiredate,1,4)||'-'||substr(hiredate,6,2)||'-'||substr(hiredate,9,2))) = '12'")
Results |
hire_month | empno | ename | job | mgr | hiredate | sal | comm | deptno | |
0 | 12 | 7369 | SMITH | CLERK | 7902.0 | 1980/12/17 | 800 | None | 20 |
1 | 12 | 7788 | SCOTT | ANALYST | 7566.0 | 1982/12/09 | 3000 | None | 20 |
2 | 12 | 7900 | JAMES | CLERK | 7698.0 | 1981/12/03 | 950 | None | 30 |
3 | 12 | 7902 | FORD | ANALYST | 7566.0 | 1981/12/03 | 3000 | None | 20 |
7. R data.table Package
Month 함수를 사용하여서 입사일자에서 월에 해당하는 정보를 추출 후 12월('DEC')에 입사한 직월들의 정보를 출력한다.
R Programming |
%%R
DT <- data.table(emp)
DT[ toupper(month.abb[month(as.Date(emp$hiredate))]) == 'DEC', ]
Results |
empno ename job mgr hiredate sal comm deptno
1: 7369 SMITH CLERK 7902 1980-12-17 800 NA 20
2: 7788 SCOTT ANALYST 7566 1982-12-09 3000 NA 20
3: 7900 JAMES CLERK 7698 1981-12-03 950 NA 30
4: 7902 FORD ANALYST 7566 1981-12-03 3000 NA 20
8. SAS Proc SQL
입사일에서 월(Month) 부분을 추출하여서 12월에 입사한 직원들의 정보를 출력한다.
SAS Programming |
%%SAS sas
PROC SQL;
CREATE TABLE STATSAS_1 AS
select empno,
hiredate,
month(hiredate) as hire_month,
substr(put(hiredate, yymmddn.),5,2) as hirechar_mon
from emp
where month(hiredate) = 12;
QUIT;
PROC PRINT;RUN;
Results |
OBS | empno | hiredate | hire_month | hirechar_mon |
1 | 7369 | 1980-12-17 | 12 | 12 |
2 | 7788 | 1982-12-09 | 12 | 12 |
3 | 7900 | 1981-12-03 | 12 | 12 |
4 | 7902 | 1981-12-03 | 12 | 12 |
영문 약칭명;
SAS Programming |
%%SAS sas
PROC SQL;
CREATE TABLE STATSAS_3 AS
select empno,
hiredate,
month(hiredate) as hire_month,
input(put(hiredate, yymmddn.),yymmdd10.) as hirechar_mon format=yymon.,
put(hiredate, yymon.) as hire_mon_ENG,
put(hiredate, monname3.) as hire_mon_ENG1
from emp
where put(hiredate, monname3.) = 'Dec';
QUIT;
PROC PRINT;RUN;
Results |
OBS | empno | hiredate | hire_month | hirechar_mon | hire_mon_ENG | hire_mon_ENG1 |
1 | 7369 | 1980-12-17 | 12 | 1980DEC | 1980DEC | Dec |
2 | 7788 | 1982-12-09 | 12 | 1982DEC | 1982DEC | Dec |
3 | 7900 | 1981-12-03 | 12 | 1981DEC | 1981DEC | Dec |
4 | 7902 | 1981-12-03 | 12 | 1981DEC | 1981DEC | Dec |
9. SAS Data Step
SAS Programming |
%%SAS sas
DATA STATSAS_3;
SET EMP;
hire_month = month(hiredate);
hirechar_mon = substr(put(hiredate, yymmddn.),5,2);
where month(hiredate) = 12;
RUN;
PROC PRINT;RUN;
Results |
OBS | empno | ename | job | mgr | hiredate | sal | comm | deptno | hire_month | hirechar_mon |
1 | 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800 | . | 20 | 12 | 12 |
2 | 7788 | SCOTT | ANALYST | 7566 | 1982-12-09 | 3000 | . | 20 | 12 | 12 |
3 | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950 | . | 30 | 12 | 12 |
4 | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000 | . | 20 | 12 | 12 |
SAS Programming |
%%SAS sas
DATA STATSAS_4;
SET EMP;
hire_month = month(hiredate);
format hirechar_mon yymon.;
hirechar_mon = input(put(hiredate, yymmddn.),yymmdd10.);
hire_mon_ENG = put(hiredate, yymon.);
hire_mon_ENG1 = put(hiredate, monname3.);
where put(hiredate, monname3.) = 'Dec';
keep empno hire: ;
RUN;
PROC PRINT;RUN;
Results |
OBS | empno | hiredate | hire_month | hirechar_mon | hire_mon_ENG | hire_mon_ENG1 |
1 | 7369 | 1980-12-17 | 12 | 1980DEC | 1980DEC | Dec |
2 | 7788 | 1982-12-09 | 12 | 1982DEC | 1982DEC | Dec |
3 | 7900 | 1981-12-03 | 12 | 1981DEC | 1981DEC | Dec |
4 | 7902 | 1981-12-03 | 12 | 1981DEC | 1981DEC | Dec |
10. Python Dfply Package
Python Programming |
emp >> mutate(hire_month=make_symbolic(pd.to_datetime)( make_symbolic(pd.to_datetime)(X.hiredate) , format='%m').dt.month_name().str.slice(stop=3).str.upper()) >> \
filter_by(X.hire_month == 'DEC')
Results |
empno | ename | job | mgr | hiredate | sal | comm | deptno | hire_month | |
0 | 7369 | SMITH | CLERK | 7902.0 | 1980/12/17 | 800 | NaN | 20 | DEC |
7 | 7788 | SCOTT | ANALYST | 7566.0 | 1982/12/09 | 3000 | NaN | 20 | DEC |
11 | 7900 | JAMES | CLERK | 7698.0 | 1981/12/03 | 950 | NaN | 30 | DEC |
12 | 7902 | FORD | ANALYST | 7566.0 | 1981/12/03 | 3000 | NaN | 20 | DEC |
[SQL, Pandas, R Prog, Dplyr, SQLDF, PANDASQL, DATA.TABLE] SQL EMP 예제로 만나는 테이블 데이터 전처리 방법 리스트
댓글