본문 바로가기
반응형

PYTHON9

[python] 직관적으로 cumulative count 계산하기(ft. pandas, numpy) 어떤 카테고리 내에서 시간 순으로 나열되어 있는 데이터에서, 특정 신호 전까지 누적카운트를 쭉 나열하는 방법을 생각해볼 수 있다. 망상해본 것이다.(원래 망상하면서 이것 저것 구현해봐야 한다.) 오늘은 이와 비슷한? 학년-반 내에서 학생 번호가 쭉 있을 때, 결석 전까지 누적 count를 매겨보려고 한다. 글로만 보면 이해안가니까 바로 코드로 deep dive! 1. Library오늘은 numpy, pandas 이외에 dictionary를 편하게 쓸 defaultdict 빌트인 함수를 가져온다.from collections import defaultdictimport numpy as npimport pandas as pd 2. Data대충 알겠지 않는가? 5,6학년 내에 1,2,3반이 있는데 각 8번.. 2025. 3. 19.
explode의 이해: Pandas dataframe에서 list 원소를 펼치는 방법 데이터프레임 전처리를 하다 보면, 가끔 아래처럼 grade별로 number가 list로 묶여있는 데이터를 마주하게 된다.data = {'grade': [1, 2, 3], 'number': [[10, 20], [30], [40, 50, 60]]}df = pd.DataFrame(data)### grade number0 1 [10, 20]1 2 [30]2 3 [40, 50, 60]### 이런 경험이 없으면, long form으로 쫙 펼쳐서 보고 싶은데 참 난감하다. 이 경우 pandas에서는 어떻게 쉽게 해결할 수 있는지, 그리고 그것을 구현해 보는 시간을 갖도록 한다.(직접 해보는 이유는, 단순히 method 하나 들고와서 "띡" 해결하는 것이 아닌 처리 로직을 이해하기 위함이다.) 1. .. 2025. 3. 9.
Fast API Tutorial(3.5): toy project(calculator) Fast API의 기능을 간단히 익힌 상태에서, API와 python으로 간단히 웹을 만들 수 있는 streamlit을 이용해 간단한 계산기를 만들어 보자api.py: Fast api 모듈 calculator.py: 계산기 모듈 streamlit_app.py: streamlit 모듈 main.py: Fast api와 streamlit을 실행하는 메인 모듈 우리는 streamlit을 통해 웹 화면에서 x, y 두 float 값과 operator(+, -, /, *)를 입력 -> 입력된 값이 api를 통해 결과를 반환 -> 반환된 결과가 다시 streamlit 웹 화면에 게시되도록 할 예정이다. 1. Fast API이 부분에서는 API 통신을 통해 계산기 인자를 받아 결과를 뱉어내도록 만드는 것이 목표다.1.. 2024. 3. 1.
Fast API Tutorial(3): query parameters 전체 코드 from fastapi import FastAPI from enum import Enum from typing import Optional app = FastAPI() @app.get("/", description='this is our first roote') async def base_get_roote(): return {"message":"hello world"} @app.post("/") async def post(): return {"message":"hello from the post roote"} @app.put("/") async def put(): return {"message":"hello from the put roote"} @app.get("/users") async de.. 2024. 2. 24.
Fast API Tutorial(2): path parameters 전체 코드 from fastapi import FastAPI from enum import Enum app = FastAPI() @app.get("/", description='this is our first roote') async def base_get_roote(): return {"message":"hello world"} @app.post("/") async def post(): return {"message":"hello from the post roote"} @app.put("/") async def put(): return {"message":"hello from the put roote"} @app.get("/users") async def list_users(): return {"mes.. 2024. 2. 23.
Fast API Tutorial(1): introduction api 공부 기록 환경 설정 requirements.txt 파일 작성 fastapi uvicorn[standard] 실습 "message":"hellow world" 터미널에서 호출 방법: univocorn main:app from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message":"hello world"} main:app의 의미 - main.py에서의 main - app = FatAPI(), @app에서의 app 만약 main.py를 hello.py로 변경 & app명칭을 world로 변경하면? uvicorn hello:world처럼 입력하면 됨 다시 돌아와서, uvicorn main:app을 .. 2024. 2. 23.
pyqt selenium 채권 정보 크롤링 (3): python활용한 채권 데이터 크롤링 구현 라이브러리 불러오기 from selenium import webdriver from selenium.webdriver.chrome.options import Options # 옵션 from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import requests from bs4 import BeautifulSoup import pandas as pd 셀레니움 사용을 위해 selenium 및 reque.. 2023. 7. 3.
pyqt selenium 채권 정보 크롤링 (2): 채권 데이터 크롤링 전략 수립 이전 시간에는 크롤링 대상 사이트를 탐색했었는데요(다른 사이트도 선택해 비슷하게 진행가능). 저는 신한투자증권 장내 채권 사이트를 선정했었습니다. https://www.shinhansec.com/siw/wealth-management/bond-rp/590401/view.do 이번 시간에는 선정한 사이트 채권 정보를 가져오는 내용을 리뷰해볼게요. 위에 보이는 테이블은 스크롤을 내리면 자동으로 채권들이 리젠되는 구조로 되어있습니다. 테이블에 존재하는 모든 채권을 가져오려면, 맨 아래에 있는 채권까지 다 리젠이 되어야 되기 때문에, 테이블 스크롤을 가장 아래로 내리는 작업이 필요합니다. 이 과정에서 Selenium(셀레니움)을 사용해 자동으로 스크롤을 맨 아래로 내리고, 모든 요소를 긁어오겠습니다. div 태.. 2023. 6. 23.
pyqt selenium 채권 정보 크롤링 (1): 데이터 수집 사이트 탐색 안녕하세요, 오늘 소개드릴 자동화는 셀레니움을 이용한 채권 정보 자동 수집입니다. 저의 경우, 채권 투자를 하고있는데요. 앱이나 사이트에서는 단순 채권 목록을 쭉 보여주는 것에 불과하거나, 제가 사용하지 않는 부가적인 기능이 많아 사용하기 불편했습니다. 그래서, 제가 자주 보는 정보 위주로 필터링하고 정렬하는 프로그램을 만들어 보겠습니다. 우선, 채권 정보를 빠르고 쉽게 수집할 사이트를 선정합니다. 저는 신한투자증권 장내 채권 사이트를 선정했습니다. (https://www.shinhansec.com/siw/wealth-management/bond-rp/590401/view.do) 채권명, 거래량, 수익률 정도를 빠르게 스키밍하고 재무재표를 볼 생각이었습니다. (신용등급, 만기일은 나중에 추가 고려대상) .. 2023. 6. 18.
반응형