-
특정 연도 이전,이후 인덱스 찾는 방법데이터분석 2023. 3. 14. 15:59
시작 일자가 2021년 이전, 종료 일자가 2022년 이후 데이터를 집계하고자할 때,
타겟 데이터가 np.array 형태이면 아래와 같은 코드(np.where 에 조건 넣기)로 인덱스를 찾을 수 있다.
import numpy as np target = np.array([19868,16659,15513,19938,17800]) A = np.array(['2020-01-01', '2020-02-01', '2022-03-01', '2023-04-01'], dtype='datetime64') B = np.array(['2023-01-01', '2023-02-01', '2023-03-01', '2024-04-01'], dtype='datetime64') indices = np.where((A < np.datetime64('2021')) & (B > np.datetime64('2022'))) print(f' indices : {indices} \n 집계대상 : {target[indices]}') # 출력 결과 # indices : (array([0, 1]),) # 집계대상 : [19868 16659]
https://numpy.org/doc/stable/reference/generated/numpy.where.html
'데이터분석' 카테고리의 다른 글
[pandas] 검사할 키워드 여러 개 , list + any (0) 2023.03.21 [pandas] 아이디 기준으로 최근 거래 항목 3개 뽑고 싶을 때 groupby ,tail (0) 2022.11.25 [pandas], 콤마 제외하고 특수 문자 제거 ( 데이터프레임, replace, 정규표현식) (0) 2022.11.11 [pandas]데이터프레임 행 단위 연산 (apply, 조건에 맞게 처리,예외 처리) (0) 2022.11.10 [pandas] read_csv 화폐 단위 열 콤마(,) 제외하고 숫자로 읽어오기 (0) 2022.10.14