본문 바로가기
반응형

Python15

Python Pivot 완벽 정리: Long Form과 Wide Form 이해하기 데이터 분석을 하다 보면 반드시 마주하게 되는 개념이 바로 Long Form과 Wide Form이다. 특히 Python의 pandas 라이브러리에서 제공하는 pivot 기능은 이 두 형태를 자유롭게 변환하는 핵심 도구이다. 이 글에서는 pivot의 개념과 함께 Long Form과 Wide Form의 차이를 명확하게 정리한다. Long Form과 Wide Form의 개념구분long formwide form구조세로로 길게 쌓인 형태가로로 펼쳐진 형태특징변수와 값이 분리됨변수마다 컬럼이 존재활용분석, 시각화에 유리보고서, 가독성에 유리 예시로 이해하는 데이터 형태import pandas as pddf = pd.DataFrame({ 'date': ['2024-01-01', '2024-01-01', '202.. 2026. 3. 24.
[Python] Custom Curve fitting과 Residual 분석 비교하기(ft. plt.ginput) 데이터 분석을 하다 보면 특정 포인트(점)들을 직접 입력해서 내가 원하는 커브를 만들고, 동시에 기본적인 회귀모델과 비교하고 싶을 때가 있다. 오늘은 matplotlib, scikit-learn, scipy를 활용해 비교해보는 시간을 갖는다. 아래 순서대로 진행할 예정이다. 1. 원본 데이터 생성2. 사용자가 화면에서 직접 점 찍기(ginput)3. 커스텀 곡선(spline) 만들기4.기본 Polynomial Regression 곡선 만들기5. 두 곡선의 Residual 비교하기 1. 원본 데이터 생성먼저 sin(x) + cos(x)에 약간의 노이즈를 추가해 실제 측정된 것 같은 데이터를 만든다.np.random.seed(1)x_data = np.linspace(0, 10, 400)y_data = np... 2025. 11. 24.
python dictionary unpacking의 모든 것(딕셔너리 unpacking, merge, 병합) 1. Passing Dictionary to a Functiondef introduce(name, age, city): print(f"이름: {name}, 나이: {age}, 도시: {city}")person = {"name": "John", "age": 25, "city": "New York"}# Unpack dictionary into function argumentsintroduce(**person)"""Name: John, Age: 25, City: New York""" 2. Merging Dictionaries dict_a = {"a": 1, "b": 2}dict_b = {"c": 3, "d": 4}merged = {**dict_a, **dict_b}print(merged)"""{'a'.. 2025. 8. 22.
[numpy.ma] Numpy로 array masking 하는 방법 오늘은 numpy masking 방법에 대해 알아본다.masking은 array에서 일부 element를 무시하는 역할을 한다. numpy array maskingnumpy에서 조건을 만족하지 않는 배열의 일부를 연산에서 제외시키거나 표현을 하고 싶지 않을 때 사용한다.indexing은 완전히 미충족 조건인 요소를 filter 하는 것이라면 masking은 가져가되 연산에만 사용하지 않는 것이다.import numpy as npdata = np.array([10, 20, 30, 40, 50])# 예: 30보다 큰 값은 무시 (마스크 처리)masked = np.ma.masked_greater(data, 30)print(masked) # [10 20 30 -- --] # 마스킹된 값은 --로 표시됨# .. 2025. 8. 4.
Pandas map() vs apply(lambda): Same Result, Different Paths When working with pandas, it’s common to see both map() and apply(lambda) used to transform a column. In many cases, they produce the same result — but they’re not exactly the same. This post shows an example where both give the same output, then explains how they differ and when to use each. 1. Example: Producing the Same Result import pandas as pddf = pd.DataFrame({ 'grade': ['A', 'B', 'C'.. 2025. 7. 10.
[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.
반응형