Tabular 분석에서 가장 유용한 단일 패턴
Split-apply-combine idiom — row 를 그룹으로 split, 각 그룹에 함수 apply, 그룹별 결과를 최종 테이블로 combine — 이 실전 tabular 질문의 절대 다수를 커버해. Pandas 가 df.groupby(...) 로 노출하고, 잘 배우는 게 Pandas 사용자로서 할 수 있는 가장 leverage 높은 일 중 하나야.
"Apply" 의 세 가지 풍미
- Aggregate — 그룹당 한 값 (sum, mean, median, count). 결과는 그룹당 한 row.
- Transform — 그룹 안에서 계산된 row 당 한 값 (각 store 안의 z-score, 각 customer 안의 rank). 결과는 입력과 같은 모양.
- Filter — 그룹 단위 테스트로 그룹 전체를 keep/drop ("이번 달 주문 100건 넘는 store 만").
Named aggregation 이 modern 스타일
옛 코드는 dict-of-list (.agg({'amount': ['sum', 'mean']})) 써서 못생긴 multi-index column 만들어. 새 코드는 named aggregation: .agg(revenue=('amount_usd', 'sum'), orders=('order_id', 'nunique')). 결과 column 이 명확한 이름 받고, 코드가 consumer 와의 contract 처럼 읽혀.