포스팅 목차
72. Display the information from EMP table. Wherever job ‘manager’ is found it should be displayed as boss(replace function).
* 직무 변수에서 직무명 'manager'를 'boss'로 변경하시오.
- 파이썬 & R 패키지 호출 및 예제 데이터 생성 링크
- [문자 함수] 문자열 변경
|
1. Oracle(오라클)
- replace 함수를 사용하여서 직무명 'MANAGER'를 'Boss'로 변경한다.
Oracle Programming |
select empno, ename, job,
replace(job, 'MANAGER', 'Boss') JOB_replace
from emp;
2. Python Pandas(파이썬)
- pandas.Series.str.replace 함수를 사용하여서 직무명 'MANAGER'를 'Boss'로 변경한다.
Python Programming |
import copy
withmooc = copy.copy(emp)
withmooc
withmooc['job_replace'] = withmooc['job'].str.replace('MANAGER','Boss')
display(withmooc.head(6))
Results |
empno | ename | job | mgr | hiredate | sal | comm | deptno | job_replace | |
0 | 7369 | SMITH | CLERK | 7902.0 | 1980/12/17 | 800 | NaN | 20 | CLERK |
1 | 7499 | ALLEN | SALESMAN | 7698.0 | 1981/02/20 | 1600 | 300.0 | 30 | SALESMAN |
2 | 7521 | WARD | SALESMAN | 7698.0 | 1981/02/22 | 1250 | 500.0 | 30 | SALESMAN |
3 | 7566 | JONES | MANAGER | 7839.0 | 1981/04/02 | 2975 | NaN | 20 | Boss |
4 | 7654 | MARTIN | SALESMAN | 7698.0 | 1981/09/28 | 1250 | 1400.0 | 30 | SALESMAN |
5 | 7698 | BLAKE | MANAGER | 7839.0 | 1981/03/01 | 2850 | NaN | 30 | Boss |
3. R Programming (R Package)
- string Vs Base 함수 : https://stringr.tidyverse.org/articles/from-base.html
base::gsub 함수를 사용하여서 직무명 'MANAGER'를 'Boss'로 변경한다.
R Programming |
%%R
withmooc <- emp
withmooc['job_replace'] = base::gsub("MANAGER", "Boss", withmooc$job, fixed=TRUE)
head(withmooc)
Results |
# A tibble: 6 x 9
empno ename job mgr hiredate sal comm deptno job_replace
<dbl> <chr> <chr> <dbl> <date> <dbl> <dbl> <dbl> <chr>
1 7369 SMITH CLERK 7902 1980-12-17 800 NA 20 CLERK
2 7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30 SALESMAN
3 7521 WARD SALESMAN 7698 1981-02-22 1250 500 30 SALESMAN
4 7566 JONES MANAGER 7839 1981-04-02 2975 NA 20 Boss
5 7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30 SALESMAN
6 7698 BLAKE MANAGER 7839 1981-03-01 2850 NA 30 Boss
stringi::stri_locate_all 함수를 사용하여서 직무명 'MANAGER'를 'Boss'로 변경한다.
R Programming |
%%R
withmooc <- emp
withmooc['job_replace'] = lapply(withmooc['job'], function(x) str_replace_all(x, "MANAGER" , "Boss") )
head(withmooc)
Results |
# A tibble: 6 x 9
empno ename job mgr hiredate sal comm deptno job_replace
<dbl> <chr> <chr> <dbl> <date> <dbl> <dbl> <dbl> <chr>
1 7369 SMITH CLERK 7902 1980-12-17 800 NA 20 CLERK
2 7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30 SALESMAN
3 7521 WARD SALESMAN 7698 1981-02-22 1250 500 30 SALESMAN
4 7566 JONES MANAGER 7839 1981-04-02 2975 NA 20 Boss
5 7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30 SALESMAN
6 7698 BLAKE MANAGER 7839 1981-03-01 2850 NA 30 Boss
4. R Dplyr Package
stringi::stri_locate_all 함수를 사용하여서 직무명 'MANAGER'를 'Boss'로 변경한다.
R Programming |
%%R
emp %>%
dplyr::mutate(job_replace = str_replace_all( job, "MANAGER" , "Boss" )) %>%
head()
Results |
# A tibble: 6 x 9
empno ename job mgr hiredate sal comm deptno job_replace
<dbl> <chr> <chr> <dbl> <date> <dbl> <dbl> <dbl> <chr>
1 7369 SMITH CLERK 7902 1980-12-17 800 NA 20 CLERK
2 7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30 SALESMAN
3 7521 WARD SALESMAN 7698 1981-02-22 1250 500 30 SALESMAN
4 7566 JONES MANAGER 7839 1981-04-02 2975 NA 20 Boss
5 7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30 SALESMAN
6 7698 BLAKE MANAGER 7839 1981-03-01 2850 NA 30 Boss
gsub 함수를 사용하여서 직무명 'MANAGER'를 'Boss'로 변경한다.
R Programming |
%%R
emp %>%
dplyr::mutate(job_replace = gsub("MANAGER", "Boss", job, fixed=TRUE)) %>%
head()
Results |
# A tibble: 6 x 9
empno ename job mgr hiredate sal comm deptno job_replace
<dbl> <chr> <chr> <dbl> <date> <dbl> <dbl> <dbl> <chr>
1 7369 SMITH CLERK 7902 1980-12-17 800 NA 20 CLERK
2 7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30 SALESMAN
3 7521 WARD SALESMAN 7698 1981-02-22 1250 500 30 SALESMAN
4 7566 JONES MANAGER 7839 1981-04-02 2975 NA 20 Boss
5 7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30 SALESMAN
6 7698 BLAKE MANAGER 7839 1981-03-01 2850 NA 30 Boss
5. R sqldf Package
replace 함수를 사용하여서 직무명 'MANAGER'를 'Boss'로 변경한다.
R Programming |
%%R
sqldf("select empno, ename,
job,
replace(job, 'MANAGER', 'Boss') job_replace
from emp;") %>% head()
Results |
empno ename job job_replace
1 7369 SMITH CLERK CLERK
2 7499 ALLEN SALESMAN SALESMAN
3 7521 WARD SALESMAN SALESMAN
4 7566 JONES MANAGER Boss
5 7654 MARTIN SALESMAN SALESMAN
6 7698 BLAKE MANAGER Boss
6. Python pandasql Package
- replace 함수를 사용하여서 직무명(Job)에서 'MANAGER'를 'Boss'로 변경한다.
Python Programming |
ps.sqldf("select empno, ename, replace(job, 'MANAGER', 'Boss') JOB from emp;").head()
Results |
empno | ename | JOB | |
0 | 7369 | SMITH | CLERK |
1 | 7499 | ALLEN | SALESMAN |
2 | 7521 | WARD | SALESMAN |
3 | 7566 | JONES | Boss |
4 | 7654 | MARTIN | SALESMAN |
7. R data.table Package
stringi::stri_locate_all 함수를 사용하여서 직무명 'MANAGER'를 'Boss'로 변경한다.
R Programming |
%%R
DT <- data.table(emp)
DT[,ename_str := str_replace_all( job, "MANAGER" , "Boss" )][1:7, ]
Results |
empno ename job mgr hiredate sal comm deptno ename_str
1: 7369 SMITH CLERK 7902 1980-12-17 800 NA 20 CLERK
2: 7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30 SALESMAN
3: 7521 WARD SALESMAN 7698 1981-02-22 1250 500 30 SALESMAN
4: 7566 JONES MANAGER 7839 1981-04-02 2975 NA 20 Boss
5: 7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30 SALESMAN
6: 7698 BLAKE MANAGER 7839 1981-03-01 2850 NA 30 Boss
7: 7782 CLARK MANAGER 7839 1981-01-09 2450 NA 10 Boss
8. SAS Proc SQL
- Tranwrd()함수, Prxchange() 함수를 사용하여서 직무명(Job)에서 'MANAGER'를 'Boss'로 변경한다.
SAS Programming |
%%SAS sas
PROC SQL;
CREATE TABLE STATSAS_1 AS
select empno, job,
tranwrd(job, 'MANAGER', 'Boss') as job_tranwrd,
prxchange('s/MANAGER/Boss/', -1, job) as job_prxchange
from emp;
QUIT;
PROC PRINT data=STATSAS_1(obs=3);RUN;
Results |
OBS | empno | job | job_tranwrd | job_prxchange |
1 | 7369 | CLERK | CLERK | CLERK |
2 | 7499 | SALESMAN | SALESMAN | SALESMAN |
3 | 7521 | SALESMAN | SALESMAN | SALESMAN |
9. SAS Data Step
- Tranwrd()함수, Prxchange() 함수를 사용하여서 직무명(Job)에서 'MANAGER'를 'Boss'로 변경한다.
SAS Programming |
%%SAS sas
DATA STATSAS_2;
SET emp;
job_tranwrd = tranwrd(job, 'MANAGER', 'Boss');
job_prxchange = prxchange('s/MANAGER/Boss/', -1, job);
keep ename empno job_tranwrd job_prxchange;
RUN;
PROC PRINT data=STATSAS_2(obs=3);RUN;
Results |
OBS | empno | ename | job_tranwrd | job_prxchange |
1 | 7369 | SMITH | CLERK | CLERK |
2 | 7499 | ALLEN | SALESMAN | SALESMAN |
3 | 7521 | WARD | SALESMAN | SALESMAN |
10. Python Dfply Package
- replace 함수를 사용하여서 직무명(Job)에서 'MANAGER'를 'Boss'로 변경한다.
Python Programming |
emp >> \
mutate( job_replace = X.job.str.replace('MANAGER','Boss') ) >> head()
Results |
empno | ename | job | mgr | hiredate | sal | comm | deptno | job_replace | |
0 | 7369 | SMITH | CLERK | 7902.0 | 1980/12/17 | 800 | NaN | 20 | CLERK |
1 | 7499 | ALLEN | SALESMAN | 7698.0 | 1981/02/20 | 1600 | 300.0 | 30 | SALESMAN |
2 | 7521 | WARD | SALESMAN | 7698.0 | 1981/02/22 | 1250 | 500.0 | 30 | SALESMAN |
3 | 7566 | JONES | MANAGER | 7839.0 | 1981/04/02 | 2975 | NaN | 20 | Boss |
4 | 7654 | MARTIN | SALESMAN | 7698.0 | 1981/09/28 | 1250 | 1400.0 | 30 | SALESMAN |
[SQL, Pandas, R Prog, Dplyr, SQLDF, PANDASQL, DATA.TABLE] SQL EMP 예제로 만나는 테이블 데이터 처리 방법 리스트
반응형
'통계프로그램 비교 시리즈 > 프로그래밍비교(Oracle,Python,R,SAS)' 카테고리의 다른 글
[데이터 전처리- 날짜함수 예제] 날짜 함수를 사용한 연령계산(나이계산) & Round 함수(반올림 함수) - 74 (0) | 2021.08.30 |
---|---|
[데이터 전처리- 문자함수 예제] 코드테이블 결합과 조건문 - 73 (0) | 2021.08.30 |
[데이터 전처리- 문자함수 예제] 문자열에서 특정 문자 변경 - 71 (0) | 2021.08.30 |
[데이터 전처리- 문자함수 예제] 기준 문자열에서 특정 문자 검색 - 70 (0) | 2021.08.30 |
[데이터 전처리- 문자함수 예제] 문자열 절단 함수를 사용하여 문자열 자르기 - 69 (0) | 2021.08.27 |
댓글