어떤 데이터가 있을때 어떤 컬럼의 데이터들끼리 어떤 조건들로 찾거나 계산할때
import time
import pandas as pd
import numpy as np
np.random.seed(0) # 난수 생성 시 일관성을 위해 시드 설정
### 1) 데이터 생성
data_size = 2000
data = {
'c1': np.random.randint(50, 201, size=data_size), # 50에서 200 사이의 정수, 100개
'c2': np.random.randint(50, 201, size=data_size),
'c3': np.random.randint(50, 201, size=data_size),
'c4': np.random.randint(50, 201, size=data_size),
'c5': np.random.randint(50, 201, size=data_size),
'c6': np.random.randint(50, 201, size=data_size),
'c7': np.random.randint(50, 201, size=data_size),
'c8': np.random.randint(50, 201, size=data_size),
'c9': np.random.randint(50, 201, size=data_size),
'c10': np.random.randint(50, 201, size=data_size)
}
# DataFrame 생성
df = pd.DataFrame(data)
### 2) 조건의 조합(순열) 만들기
col = ['c1','c2','c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10']
lst = list(itertools.permutations(col, 2)) # 순열 만들기. [('c1', 'c2'), ('c1', 'c3'), ('c1', 'c4'), ('c1', 'c5'), ('c1', 'c6')...
start_time = time.time() # 시작시간 저장
combinations1 = []
### 3) 데이터에서 조건으로 찾기
idx = 0
max_idx = 0
max_count = 0
max_case1 = []
max_case2 = []
for item1 in lst: # 조건 1번째. 어떤 df의 컬럼을 사용할 것인지
for item2 in lst: # 조건 2번째
count1 = []
count2 = []
for i in range(len(df)): # 데이터의 row별 조건 확인을 위한 반복문
data1 = df[item1[0]].iloc[i] # 컬럼명의 항목값
data2 = df[item1[1]].iloc[i]
data3 = df[item2[0]].iloc[i]
data4 = df[item2[1]].iloc[i]
if data1 > data2: # 데이터의 조건 판단
count1.append(idx)
elif data3 > data4:
count2.append(idx)
count_sum = len(count1) + len(count2) # 조건 결과
if count_sum > max_count: # 조건 결과로 max 구하기
max_idx = idx
max_count = count_sum
max_case1 = item1
max_case2 = item2
# print(f"[{idx}] {item1} : {item2} : {count_sum} : {max_idx} : {max_count}")
combinations1.append([idx, item1, item2])
idx += 1
### 4) 출력
print(f"Best case : {max_count:,} (idx={best_idx}), {max_case1}:{max_case2}")
# 실행기간 계산 및 출력
end_time = time.time()
elapsed = end_time - start_time
minutes = int(elapsed // 60)
seconds = int(elapsed % 60)
print(f"\nRunning time: {minutes}m {seconds}sec")
다음에 속도 개선
https://question99.tistory.com/1096
[파이썬] 조건에 맞는 데이터 계산(Parallel, 병렬처리, 다중프로세스)
https://question99.tistory.com/1095 [파이썬] 조건에 맞는 데이터 계산(다중 for문)어떤 데이터가 있을때 어떤 컬럼의 데이터들끼리 어떤 조건들로 찾거나 계산할때import timeimport pandas as pdimport numpy as npnp.r
question99.tistory.com
https://question99.tistory.com/1097
[파이썬] 조건에 맞는 데이터 계산(Numba사용)
https://question99.tistory.com/1095 [파이썬] 조건에 맞는 데이터 계산(다중 for문)어떤 데이터가 있을때 어떤 컬럼의 데이터들끼리 어떤 조건들로 찾거나 계산할때import timeimport pandas as pdimport numpy as npnp.r
question99.tistory.com
https://question99.tistory.com/1098
[파이썬] 조건에 맞는 데이터 계산(병렬처리 하지 않고 Numba만 사용)
https://question99.tistory.com/1095 [파이썬] 조건에 맞는 데이터 계산(다중 for문)어떤 데이터가 있을때 어떤 컬럼의 데이터들끼리 어떤 조건들로 찾거나 계산할때import timeimport pandas as pdimport numpy as npnp.r
question99.tistory.com
'프로그램' 카테고리의 다른 글
[파이썬] 680배 빨라진다! 조건에 맞는 데이터 계산(병렬처리 하지 않고 Numba만 사용) (0) | 2025.06.03 |
---|---|
[파이썬] 150배 빨라진다! 조건에 맞는 데이터 계산(병렬처리와 Numba사용) (0) | 2025.06.03 |
[A.I] OpenManus(chatGPT) 사용하기 (2) | 2025.04.13 |
[A.I] 파이썬으로 ollama에 접속하기 (0) | 2025.04.11 |
[딥러닝] 지도학습 후 강화학습 (DeepSeek 알고리즘) (0) | 2025.02.04 |
댓글