count(), sum()같은 집계함수의 경우 인자값으로 칼럼 뿐만 아니라 case when
절을 넣어 조건에 따른 집계함수 결과값을 반환할 수 있다.
count(case when rating < 3 then 1 end)
: 3점 미만의 순위 숫자
sum(case when rating < 3 then 1 end)
: 3점 미만의 값의 수
두 개의 반환값은 동일하다.
sum(case when state = 'approved' then 1 else 0 end) from transactions
: 승인된 건 수 구하기
WHERE IN
를 통해 특정 값이 목록에 포함되는지 확인하여 조건에 맞는 행을 필터링할 수 있다. 이 기능을 확장하여 복수의 컬럼 역시 필터링 가능하다.
where A in B
select *
from table_1
where column1 in (select column1 from table_2);
where (a,b) in (A,B)
튜플
을 사용하면 된다. select *
from table_1
where (column1, column2) in (select column1, column2 from table_2)
random.rand(d0, d1, d2, ...,dn)
: 0~1 균일분포를 따르는 array 생성. print(np.random.rand(3,2)) # 3행 2열
array([[ 0.14022471, 0.96360618], #random
[ 0.37601032, 0.25528411], #random
[ 0.49313049, 0.94909878]]) #random
random.randint(low, high=None, size=None, dtype=int)
: low이상 high미만 값의 무작위 정수를 리턴. high값이 없으면(디폴트는 없다) 0이상 low값 미만 범위의 무작위 정수를 리턴한다.size
가 정수면 생성할 정수 개수를 리턴하며, 튜플이면 행렬의 형태를 뜻한다. -> size(2,3)은 2행 3열 형태이다. import numpy as np
print(np.random.randint(10) #0이상10미만의 정수 생성
print(np.random.randint(5,15)) #5이상 100미만의 정수 생성
print(np.random.randint(1, 100, size=5) # 1이상 100미만의 정수 5개 생성
print(np.random.randint(1, 100, size=(2,3))) # 1 이상 100미만의 정수로 2행 3렬 매트릭스 생성
print(np.random.randint(1,10,size=3,dtype=np.int64)) #1이상10미만의 64비트 정수 3개 생성
random.randn(d0, d1, ..., dn)
: 표준 정규분포(평균0,표준편차1)에서 난수 생성d
인자는 하나면 원소 개수, 복수면 차원을 의미한다. import numpy as np
print(np.random.randn(5)) # 5개의 원소를 가지는 1차원 배열 생성
import numpy as np
print(np.random.randn(2, 3, 4)) # 2x3x4 형태의 3차원 배열 생성