본문 바로가기
통계프로그램 비교 시리즈/프로그래밍비교(Oracle,Python,R,SAS)

[데이터 추출] 그룹 집계 후 특정 조건을 만족하는 그룹의 집계 통계량 출력 - 109

by 기서무나구물 2022. 12. 5.

 

109. Display count of employees in each department where count greater than 3.

 

* 부서내 직원 수가 4명 이상인 부서의 부서명과 직원수를 출력하시오.


  • 그룹 집계 후 특정 조건을 만족하는 그룹의 집계 통계량 출력
  • Oracle : group by, having count(*)
  • 파이썬 Pandas : groupby(), query(), agg(), filter(), lambda 함수
  • R 프로그래밍 : aggregate(), subset(), FUN = function(x) 사용자 정의 함수, list(), tapply()
  • R Dplyr Package : group_by(), dplyr::summarise, n(), dplyr::tally(), dplyr::count(), nrow(.), do(), dplyr::filter()
  • R sqldf Package : group by, having count(*)
  • Python pandasql Package : group by, having count(*)
  • R data.table Package : .SD (Subset of Dataframe), keyby =, .N
  • SAS Proc SQL : group by, having count(*)
  • SAS Data Step : PROC SUMMARY, FIRST. and LAST.
  • Python Dfply Package : group_by, summarize()
  • 파이썬 Base 프로그래밍 :

 


1. Oracle(오라클)

부서별 직원의 수를 카운트 후 부서내 직원 수(집계 결과)가 4명 이상인 부서의 부서번호와 개별 부서의 직원수를 출력한다.

 

Oracle Programming
select deptno, count(distinct empno) as emp_cnt 
from   emp 
group 
   by  deptno 
having count(*) > 3;

 


2. Python Pandas(파이썬)

부서별 직원의 수를 카운트 후 query() 함수를 사용하여서 부서내 직원 수가 4명 이상인 부서의 부서명과 직원수를 출력한다.

 

Python Programming
emp.groupby('deptno')['empno'].count().reset_index().query('empno > 3')

 


Results
  deptno empno
1 20 5
2 30 6

 


부서별 직원의 수를 카운트 후 query() 함수를 사용하여서 부서내 직원 수가 4명 이상인 부서의 부서명과 직원수를 출력한다.

 

Python Programming
emp.groupby('deptno').agg({'empno':'count'}).reset_index().query('empno > 3')

 

Results

  deptno empno
1 20 5
2 30 6

 


filter 함수를 사용하여서 부서별 직원의 수가 4명 이상인 직원 정보 리스트를 1차적으로 선택 후 다시 부서별 직원수를 카운트한다.

 

Python Programming
emp.groupby('deptno').filter(lambda x: x['empno'].count() > 3).groupby('deptno')['empno'].count().reset_index()

 

 
Results
  deptno empno
0 20 5
1 30 6

 


3. R Programming (R Package)

부서별 직원의 수를 카운트 후 subset() 함수를 사용하여서 부서내 직원 수가 4명 이상인 부서의 부서명과 직원수를 출력한다.

 

R Programming
%%R

subset( aggregate(empno~deptno, emp, length),
        empno>3 )

 

Results
  deptno empno
2     20     5
3     30     6

 


부서별 직원의 수를 카운트 후 subset() 함수를 사용하여서 부서내 직원 수가 4명 이상인 부서의 부서명과 직원수를 출력한다.

 

R Programming
%%R

subset( aggregate(empno ~ deptno, data = emp, FUN = function(x) c(emp_cnt = length(x) ) ), 
        empno >3 )

 

Results
  deptno empno
2     20     5
3     30     6

 


 

R Programming
%%R

aggregate(emp$empno,list(deptno_gr=emp$deptno),count)

 

Results
  deptno_gr count.emp$empno
1        10               3
2        20               5
3        30               6

 


부서별 관측치 건수를 카운트 후 subset() 함수를 사용하여서 부서별 자료 개수(직원의 수)가 4건 이상인 부서의 부서명과 직원수를 출력한다. tapply() 함수는 벡터(Vector)의 개별 subset(그룹)별로 사용자가 지정한 함수를 계산하기 위한 함수이다.

 

R Programming
%%R

subset( tapply(emp$sal,emp$deptno,length), tapply(emp$sal,emp$deptno,length)>3 )

 

Results
20 30 
 5  6 

 


4. R Dplyr Package

summarise() 함수를 지정하여 부서별 관측치 건수를 카운트 후 filter() 함수를 사용하여서 부서별 자료 개수(직원의 수)가 4건 이상인 부서의 부서명과 직원수를 출력한다.

 

R Programming
%%R

emp%>%
  dplyr::group_by(deptno)%>%
  dplyr::summarise(sumsal=n()) %>%
  dplyr::filter(sumsal > 3)

 

Results
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 2 x 2
  deptno sumsal
   <dbl>  <int>
1     20      5
2     30      6

 


tally() 함수를 사용하여 부서별 관측치 건수를 계산한 후에 filter() 함수를 사용하여서 부서별 자료 개수(직원의 수)가 4건 이상인 부서의 부서명과 직원수를 출력한다.

 

R Programming
%%R

emp%>%
  dplyr::group_by(deptno) %>%
  dplyr::tally() %>%
  dplyr::filter(n >3)

 

Results
# A tibble: 2 x 2
  deptno     n
   <dbl> <int>
1     20     5
2     30     6

 


count() 함수를 사용하여 함수내 그룹 변수를 직접 지정하여 부서별 관측치 건수를 계산한 후에 filter() 함수를 사용하여서 부서별 자료 개수(직원의 수)가 4건 이상인 부서의 부서명과 직원수를 출력한다.

 

R Programming
%%R

emp%>%
  dplyr::count(deptno) %>%
  dplyr::filter(n >3)

 

Results
# A tibble: 3 x 2
  deptno     n
   <dbl> <int>
1     10     3
2     20     5
3     30     6

 


그룹별 연산을 수행하기 위하여 do 함수를 사용하여 부서별 관측치 건수를 계산한 후에 filter() 함수를 사용하여서 부서별 자료 개수(직원의 수)가 4건 이상인 부서의 부서명과 직원수를 출력한다. Do 함수를 사용하여서 그룹별로 다양하고 복잡한 작업을 수행 할 수 있다.

 

R Programming
%%R

emp %>% 
  dplyr::group_by(deptno) %>%
  do(data.frame(nrow=nrow(.))) %>%
  dplyr::filter(nrow >3)

 

Results
# A tibble: 2 x 2
# Groups:   deptno [2]
  deptno  nrow
   <dbl> <int>
1     20     5
2     30     6

 


5. R sqldf Package

부서별 직원의 수를 카운트 후 부서내 직원 수가 4명 이상인 부서의 부서명과 직원수를 출력한다.

 

R Programming
%%R

sqldf(" select deptno, count(distinct empno) as emp_cnt 
        from   emp 
        group 
           by  deptno 
        having count(*)>3")

 

Results
  deptno emp_cnt
1     20       5
2     30       6

 


6. Python pandasql Package

부서별 직원의 수를 카운트 후 부서내 직원 수(집계 결과)가 4명 이상인 부서의 부서명과 직원수를 출력한다.

 

Python Programming
ps.sqldf("select deptno, count(*) emp_cnt from emp group by deptno having count(*)>3;")

 


Results
  deptno emp_cnt
0 20 5
1 30 6

 


7. R data.table Package

그룹별 연산을 수행하기 위하여 .SD 연산자(Subset of Data) 를 지정하여 부서별로 관측치 건수를 계산한 후에 부서별 자료 개수(직원의 수)가 4건 이상인 부서의 부서명과 직원수를 출력한다.

 

R Programming
%%R

DT <- data.table(emp)

DT[, .SD[.N > 3, .('emp_cnt' = .N)], keyby = .(deptno)]

 

Results
   deptno emp_cnt
1:     20       5
2:     30       6

 


.N 함수를 사용하여 부서별 관측치 건수를 계산한 후에 filter() 함수를 사용하여서 부서별 자료 개수(직원의 수)가 4건 이상인 부서의 부서명과 직원수를 출력한다.

 

R Programming
%%R

DT <- data.table(emp)

DT[, .N, by = .(deptno)][N > 3]

 

Results
   deptno N
1:     20 5
2:     30 6

 


8. SAS Proc SQL

부서별 직원의 수를 카운트 후 부서내 직원 수(집계 결과)가 4명 이상인 부서의 부서번호와 직원수를 출력한다.

 

SAS Programming
%%SAS sas

PROC SQL;
  CREATE TABLE STATSAS_1 AS
    select deptno, count(*) as EMPNO_CNT
    from   emp 
    group 
       by  1
    having count(*)>3;
QUIT;
PROC PRINT;RUN;

 


Results
OBS deptno EMPNO_CNT
1 20 5
2 30 6

 


9. SAS Data Step

 

SAS Programming
%%SAS sas

PROC SUMMARY DATA=EMP NWAY;
     CLASS DEPTNO;
     VAR EMPNO;
     OUTPUT OUT=STATSAS_2(DROP=_: WHERE=(EMPNO_CNT>3)) N=EMPNO_CNT;
RUN;
PROC PRINT;RUN;

 


Results
OBS deptno EMPNO_CNT
1 20 5
2 30 6

 


 

SAS Programming
%%SAS sas

PROC SORT DATA=EMP OUT=EMP_1;
     BY DEPTNO;
RUN;

DATA STATSAS_2;
 SET EMP_1;
     BY DEPTNO;
     IF FIRST.DEPTNO THEN EMPNO_CNT = 1;
     ELSE EMPNO_CNT+1;

     IF LAST.DEPTNO = 1 AND EMPNO_CNT > 3 THEN OUTPUT;
     KEEP DEPTNO EMPNO_CNT;
RUN;
PROC PRINT;RUN;

 


Results
OBS deptno EMPNO_CNT
1 20 5
2 30 6

 


10. Python Dfply Package

 

Python Programming
emp >> group_by(X.deptno) >> summarize(emp_cnt = n(X.empno)) >> filter_by(X.emp_cnt > 3)

 

Results

  deptno emp_cnt
1 20 5
2 30 6

 


emp >> group_by(X.deptno) >> summarize(emp_cnt = X.empno.count()) >> filter_by(X.emp_cnt > 3)

 

  deptno emp_cnt
1 20 5
2 30 6

 

 


[SQL, Pandas, R Prog, Dplyr, SQLDF, PANDASQL, DATA.TABLE]   SQL EMP 예제로 만나는 테이블 데이터 전처리 방법 리스트

반응형

댓글