Python - bisect 예제
·
Programming/Python
[ bisect 모듈 ]bisect 모듈은 이진 탐색을 쉽게 쓸 수 있게 해주는 파이썬 표준 라이브러리"정렬된 리스트"에 값을 효율적으로 삽입하거나 위치를 찾을 때 유용 [ 사용 함수 ]함수의미반환값bisect_left(a, x)좌측 삽입 위치 탐색x를 a에 넣을 때 왼쪽 인덱스bisect_right(a, x)우측 삽입 위치 탐색x를 a에 넣을 때 오른쪽 인덱스insort_left(a, x)bisect_left 위치에 삽입리스트 a가 정렬된 상태 유지insort_right(a, x)bisect_right 위치에 삽입동일 [ 예제 ]정렬 리스트에 중복을 허용하며 요소 삽입import bisectscores = [15, 22, 22, 30]bisect.insort(scores, 22) # ins..
Python - Counter
·
Programming/Python
생성 및 확인from collections import Counter# 리스트 요소 개수 세기nums = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]counter = Counter(nums)print(counter) # Counter({4: 4, 3: 3, 2: 2, 1: 1})# 문자열 문자 개수 세기text = "hello world"counter = Counter(text)print(counter) # Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})# 키인덱스counter['l'] # 2counter.values()# dict_values([1, 1, 3, 2, 1, 1, 1, 1]) Counter 직..
Python - io.BytesIO 예제
·
Programming/Python
io.BytesIO란"io.BytesIO"는 바이트 기반의 I/O 작업을 수행할 때 유용합니다.파일을 다루는 것과 동일한 인터페이스를 제공하지만,실제 파일 시스템이 아닌 메모리에서 바이트 데이터를 처리합니다. 주요기능메모리에서 바이트 데이터를 처리: 데이터를 메모리 내에서 처리하므로,  빠르게 바이트 데이터를 임시로 저장하고 조작파일과 같은 인터페이스: 파일 객체와 동일한 메서드(예: read(), write(), seek())를 제공, 실제 파일처럼 데이터를 처리  사용 예시1. BytesIO 객체에 바이트 데이터 쓰기 및 읽기import io# BytesIO 객체 생성byte_stream = io.BytesIO()# 바이트 데이터를 메모리 내에 쓰기byte_stream.write(b'Hello, By..
Python - Pandas 예제2 (Apply,Map,Time)
·
Programming/Python
1. Apply & Mapimport pandas as pd### ###### 04 Apply & Map ###### ###df= pd.read_csv('https://raw.githubusercontent.com/Datamanim/pandas/main/BankChurnersUp.csv')# Income_Category의 카테고리를 map 함수를 이용하여 다음과 같이 변경하여 newIncome 컬럼에 매핑하라dic = { 'Unknown' : 'N', 'Less than $40K' : 'a', '$40K - $60K' : 'b', '$60K - $80K' : 'c', '$80K - ..
Python - Pandas 예제1 (Filtering,Sorting,Grouping)
·
Programming/Python
1. Getting- dir, shape, columns, info, dtype, iloc# 롤 랭킹 데이터 : https://www.kaggle.com/datasnaek/league-of-legends# DataUrl = ‘https://raw.githubusercontent.com/Datamanim/pandas/main/lol.csv’import pandas as pd### ###### 01 Getting & Knowing Data ###### #### 데이터 로드, 데이터는 \t을 기준으로 구분df = pd.read_csv("https://raw.githubusercontent.com/Datamanim/pa..
Python - Pandas 기본
·
Programming/Python
1.Basic데이터 프레임 만들기import pandas as pddf1 = pd.DataFrame( [[3,2,5],[10,0,2],[6,5,3]], columns=["사과", "자두", "포도"], index=["이성계", "김유신", "이순신"]) values, index, columnsdf1.values# array([[ 3, 2, 5],# [10, 0, 2],# [ 6, 5, 3]])df1.index# Index(['이성계', '김유신', '이순신'], dtype='object')df1.columns# Index(['사과', '자두', '포도'], dtype='object')## numpy와 같이 pandas도 value를 조건에 따라 bool..
Python - AWS Athena 쿼리실행방법 PyAthena vs Boto3
·
Programming/Python
python에서 AWS Athena의 query를 활용하는데는 보편적으로 2가지 방법이 있다.하나는 pyathena 라이브러리의 connect.cursor()를 사용하는 방법이고, 다른 하나는 Boto3의 start_query_execution API를 사용하는 방법이다.  pyathena.connect 사용pyathena는 AWS Athena와의 상호작용을 단순화한 라이브러리로,SQL 쿼리 실행 및 결과 조회를 간편하게 할 수 있다.from pyathena import connectimport pandas as pd# Athena에 연결conn = connect( s3_staging_dir='s3://your-bucket/path/', # 쿼리 결과를 저장할 S3 버킷 경로 region_n..
Python - Transpose(전치) 예제
·
Programming/Python
Transpose 란Transpose는 행렬이나 2차원 리스트에서 행(row)과 열(column)을 서로 맞바꾸는 작업을 의미합니다.예를 들어, 𝑛 × 𝑚 행렬에서 전치된 행렬은 𝑚 × 𝑛 크기를 가지며,원래 행렬의 행이 열이 되고, 열이 행이 됩니다.  Transpose 구현Python에서 전치를 구현하는 일반적인 방법 중 하나는 zip 함수와 * 연산자를 사용하는 것입니다. zip(*iterables)의 동작- zip 함수는 여러 iterable(리스트, 튜플 등)의 요소를 묶어서 튜플을 생성하는 함수입니다.- '*' 연산자를 통해 iterable의 각 요소를 개별 인자로 풀어서 함수에 전달합니다.  Transpose 예시# 예시 2차원 리스트 (grid)grid = [ ['a', 'b'..
wave35
'python' 태그의 글 목록