str_expr = "weight in [60,30]" # 같은 결과 "weight == [60,30]"
people_df_q = people_df.query(str_expr) # 조건 부합 데이터 추출
print("조건 : 몸무게가 30 이거나 60인 데이터")
display_side_by_side(people_df, people_df_q)
5) not in 연산자( not in, !=)
not in 과 != 의 결과는 같다
다수의 조건데이터는 리스트[] 또는 튜플() 형태로 사용가능
str_expr = "weight != 30" # 같은 결과 "weight not in 30"
people_df_q = people_df.query(str_expr) # 조건 부합 데이터 추출
print("조건 : 몸무게가 30이 아닌 데이터")
display_side_by_side(people_df, people_df_q)
6) 논리 연산자(and, or, not)
and : 전체 조건이 참일경우 참
or : 전체 조건 중 하나라도 참일경우 참
not : 뒤에 오는 조건의 반대(참일경우 거짓, 거짓일경우 참)
str_expr = "(weight > 50) and (weight <80)"
people_df_q = people_df.query(str_expr) # 조건 부합 데이터 추출
print("※ and 연산자")
print("조건 : 몸무게가 50보다 크고 80보다 작은 데이터")
display_side_by_side(people_df, people_df_q)
str_expr = "(weight == 50) or (weight > 60)"
print("※ or 연산자")
print("조건 : 몸무게가 50이거나 60보다 큰 데이터")
people_df_q = people_df.query(str_expr) # 조건 부합 데이터 추출
display_side_by_side(people_df, people_df_q)
str_expr = "not (weight == 50)"
print("※ not 연산자")
print("조건 : 몸무게가 50이 아닌 데이터")
people_df_q = people_df.query(str_expr) # 조건 부합 데이터 추출
display_side_by_side(people_df, people_df_q)
7) 외부 변수 참조 연산
num_weight = 70
num_height = 200
str_expr = "(weight > @num_weight) and (height <= @num_height)"
print("조건 : 몸무게가 70보다 크고 키가 200보다 작은 데이터")
people_df_q = people_df.query(str_expr) # 조건 부합 데이터 추출
display_side_by_side(people_df, people_df_q)
8) f-String을 이용한 외부 변수(또는 함수) 참조 연산
num_weight = 70
num_height = 200
str_expr = f"(weight > {num_weight}) and (height <= {num_height})"
people_df_q = people_df.query(str_expr) # 조건 부합 데이터 추출
print("조건 : 몸무게가 70보다 크고 키가 200보다 작은 데이터")
display_side_by_side(people_df, people_df_q)
9) 함수를 이용한 참조 연산
def weight_min(data):
return min(data)
num_weight = 70
str_expr = "weight > @weight_min([70,90,30,50])" # 몸무게가 30보다 큰 데이터
print("조건 : 몸무게가 리스트데이터([70,90,30,50]) 중 최저값보다 큰 데이터")
people_df_q = people_df.query(str_expr) # 조건 부합 데이터 추출
display_side_by_side(people_df, people_df_q)
10) 인덱스 검색
인덱스이름이 있다면 index대신 이름(DataFrameObj.index.name)을 기입합니다.
str_expr = "index > 1"
people_df_q = people_df.query(str_expr) # 조건 부합 데이터 추출
print("조건 : 인덱스(index)가 1보다 큰 데이터")
display_side_by_side(people_df, people_df_q)
11) (비교문자열)컬럼명.str.contains
▶ query 함수 옵션
engine : python
▶ contains 함수 옵션
case : True (대소문자 구별, 디폴트), False(대소문자 구별 안함)
str_expr = 'name.str.contains("a")'
people_df_q = people_df.query(str_expr, engine="python") # 조건 부합 데이터 추출
print("※ 대소문자 구별")
print("조건 : 이름에 'a'가 들어가 있는 데이터")
display_side_by_side(people_df, people_df_q)
str_expr = 'name.str.contains("a",case=False)'
people_df_q = people_df.query(str_expr, engine="python") # 조건 부합 데이터 추출
print("※ 대소문자 구별 안함")
print("조건 : 이름에 'a'가 들어가 있는 데이터")
display_side_by_side(people_df, people_df_q)
12) (비교문자열)컬럼명.str.startswith
str_expr = 'name.str.startswith("P")'
people_df_q = people_df.query(str_expr, engine="python") # 조건 부합 데이터 추출
print("조건 : 이름이 'P' 로 시작하는 데이터")
display_side_by_side(people_df, people_df_q)
13) (비교문자열)컬럼명.str.endswith
str_expr = 'name.str.endswith("m")'
people_df_q = people_df.query(str_expr, engine="python") # 조건 부합 데이터 추출
print("조건 : 이름이 'm' 로 끝나는 데이터")
display_side_by_side(people_df, people_df_q)
배열의 차원 반환 : array객체.ndim 배열의 크기 반환 : array객체.shape
II. 배열의 전체 원소의 형변환
※ dtype 접두사 종류
dtype 접두사
설명
사용 예
b
불리언
b(참 혹은 거짓)
i
정수
i8(64비트)
u
부호 없는 정수
u8(64비트)
f
부동소수점
f8(64비트)
c
복소 부동소수점
c16(128비트)
o
객체
0(객체에 대한 포인터)
S
바이트 문자열
S24(24 글자)
U
유니코드 문자열
U24(24 유니코드 글자
dtype = '자료형' or dtype = numpy.자료형
III. 전치 연산 (행과 열의 위치 변환)
array객체.T
IV. 다차원의 배열을 1차원 배열로 변환
flatten() or ravel() or reshape(-1)
V. 배열의 인덱싱
1) array객체[행 인덱스, 열 인덱스]
2) array객체[행 인덱스][열 인덱스]
VI. 배열 슬라이싱
1) index : 원하는 행 혹은 열 추출
2) 범위(:) : 시작 인덱스부터 끝 인덱스 까지 추출
VII. 배열 연결
1) hstack
행의 수가 같은 두 개 이상의 배열을 옆으로 연결하여 열의 수가 더 많은 배열을 만든다.
2) vstack
열의 수가 같은 두 개 이상의 배열을 위아래로 연결하여 행의 수가 더 많은 배열을 만든다
3) dstack
제3의 축 즉, 행이나 열이 아닌 깊이(depth) 방향으로 배열을 합친다. 가장 안쪽의 원소의 차원이 증가한다. 즉 가장 내부의 숫자 원소가 배열이 된다. shape 정보로 보자면 가장 끝에 값이 2인 차원이 추가되는 것이다.
4) stack
dstack의 기능을 확장한 것으로 dstack처럼 마지막 차원으로 연결하는 것이 아니라 사용자가 지정한 차원(축으로) 배열을 연결한다. axis인수(디폴트 0)를 사용하여 연결후의 회전 방향을 정한다. 디폴트 인수값은 0이고 가장 앞쪽에 차원이 생성된다. 즉, 배열 두 개가 겹치게 되므로 연결하고자 하는 배열들의 크기가 모두 같아야 한다.
(1) axis = 0 (디폴트)
첫번째 차원으로 새로운 차원이 삽입된다.
(1) axis = 1
두번째 차원으로 새로운 차원이 삽입된다.
5) r_
hstack 명령과 비슷하게 배열을 좌우로 연결한다. 다만 메서드임에도 불구하고 소괄호(parenthesis, ())를 사용하지 않고 인덱싱과 같이 대괄호(bracket, [])를 사용한다. 이런 특수 메서드를 인덱서(indexer)라고 한다.
6) c_
배열의 차원을 증가시킨 후 좌우로 연결한다. 만약 1차원 배열을 연결하면 2차원 배열이 된다.