본문 바로가기

AI/Python

(12)
[Pandas] DataFrame - iloc, loc 인덱서 Row (행)데이터프레임의 행 데이터를 구분하는 방법에는 index position(위치 기반) 과 index label(이름 기반) 존재한다.import pandas as pddf = pd.DataFrame({ 'name': ['Tom', 'Jane', 'Steve', 'Lucy'], 'age': [28, 31, 24, 27], 'city': ['Seoul', 'Busan', 'Incheon', 'Daegu'] }, index=['a', 'b', 'c', 'd']) # index label 지정, 없으면 default 0,1,2,,, 설정됨.dfdf의 index position은 [0,1,2,3]이고, 인덱스 이름은 ['a', 'b', 'c', 'd']이다.iloclocindex po..
[Python] Web Crawling - 정적 웹 페이지 🆚 동적 웹 페이지 📌 Crawling일단 정적 웹페이지와 동적 웹페이지가 존재하는데 그 차이를 알아야한다.정적 웹페이지는 움직이지 않고 현재 상태 그대로 html을 가져와 사용할 수 있는 사이트를 말한다.검색했을때 url 링크가 검색어가 q = "hello" 형태로 붙는다거나상세 페이지 url을 가지고 해당 상품의 상세 정보 내용을 모두 가져올 수 있는 페이지를 정적 웹페이지라고 한다.동적 웹페이지는 동일한 url인데 사용자의 컨트롤에 따라 화면의 결과, html이 달라지는 사이트를 말한다.무한 스크롤이 가능한 페이지나 tab 버튼을 눌렀는데 현재 페이지에서 하단 결과 값만 달라지거나 한다.📌 Crawling에선 뭐가 다른데??!!!정적 웹페이지는 requests 모듈을 사용하여 url의 html을 가져오는 방법을 배..
[Python] Yield 키워드와 Generator 함수 ContextManager를 공부하다가 나온 yield 키워드. [Python] 파일 읽고 쓰기 with문 - ContextManagerPython에서 자원을 읽고 쓸 때, try-except 문을 사용하지 않고 with문을 사용한다고 한다.try-except 문을 사용해서 파일을 읽고 쓰게 되면, finally에서 연결 끊는 코드를 안해주면 자원을 계속 실행하고itstudentstudy.tistory.com그리고 yield 키워드를 가지고 있는 함수를 Generator 함수라고 하는데,,yield 키워드는 뭐고? Generator 함수는 뭘까?📌 Yield 키워드def test(value): print("_start_") yield value * 10 print("_continue_"..
[Python] 파일 읽고 쓰기 with문 - ContextManager Python에서 자원을 읽고 쓸 때, try-except 문을 사용하지 않고 with문을 사용한다고 한다.try-except 문을 사용해서 파일을 읽고 쓰게 되면, finally에서 연결 끊는 코드를 안해주면 자원을 계속 실행하고 있게된다. 직접 해제해줘야함.f = open('file.txt', 'r')try: data = f.read() print(data)finally: f.close() # 리소스를 직접 해제해야 함with문을 사용하면 block을 벗어나면 f.close를 안해줘도 자동으로 연결을 해제해준다.with open('file.txt', 'r') as f: data = f.read() print(data)# with 블록을 벗어나는 순간, f.close()가 자동..
[Python] 클래스의 던더 변수 및 메소드 class T: def __init(self, num): self.num = num def __call(self, value): return self.num + value def __str__(self): return f"T > num : {self.num}" def __add__(self, value) -> int|float|None: if isinstance(value, (int|float): return self.num + value else: return None__dict__객체가 가지고 있는 변수를 Dictionary로 반환하는 함수동일 함수 이름이나 동일 변..
[Python] 클래스와 변수 + 접근지정자 getter, setter 📌 클래스 변수(Class Variable)와 인스턴스 변수(Instance Variable)클래스 변수 : 클래스가 공유하는 변수 (모든 객체가 같은 값을 가짐)인스턴스 변수 : 객체마다 독립적으로 가지는 변수class Person: # 클래스 변수 species = "Human" def __init__(self, alias): # 인스턴스 변수 self.alias = namep = Person("Tom")print(Person.species) #Human (클래스 변수) print(p.species) # Human (클래스 변수)print(p.alias) # Tom (인스턴스 변수)Person.species = "Changed" # 클래스 변수 ..
[Python] datetime 모듈 import datetime현재 시간 출력하기now = datetime.datetime.now()#datetime.datetime(2025, 9, 25, 8, 59, 22, 622938)시간 포맷 맞춰서 출력하기now.strftime("%Y.%m.%d %H:%M:%S")# 25.09.25 08:59:22now.strftime("%Y.%m.%d %H:%M:%S")# 2025.09.25 08:59:22now.strftime("%D")# 09/25/25now.strftime("%y{}%m{}%d{} %H:%M:%S").format(*"년월일")#25년09월25일 08:59:22시간 더하기 datetime — Basic date and time typesSource code: Lib/datetime.py The..
[Python] ascii code <-> string 변환 방법 📌 아스키코드 -> stringchr(s)📌 string-> 아스키코드ord(s)