■ DataFrame 클래스의 agg 메소드에서 lambda 함수를 커스텀 집계 함수로 사용하는 방법을 보여준다.
▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import pandas as pd import numpy as np datetimeIndex = pd.date_range("20130101", periods = 6) dataFrame = pd.DataFrame(np.random.randn(6, 4), index = datetimeIndex, columns = list("ABCD")) print(dataFrame) """ A B C D 2013-01-01 0.389016 1.940161 0.821590 -0.026517 2013-01-02 -0.393310 2.111965 0.392065 0.177232 2013-01-03 -0.434576 0.289613 -0.507643 0.838895 2013-01-04 0.177708 -0.698036 0.195119 -1.447753 2013-01-05 0.536391 2.427713 0.306881 0.999480 2013-01-06 2.339590 -1.860456 -1.482091 0.046616 """ print() series = dataFrame.agg(lambda x : np.mean(x) * 5.6) print(series) """ A 2.440498 B 3.930228 C -0.255807 D 0.548755 dtype: float64 """ |
▶ requirements.txt
1 2 3 4 5 6 7 8 |
numpy==2.1.2 pandas==2.2.3 python-dateutil==2.9.0.post0 pytz==2024.2 six==1.16.0 tzdata==2024.2 |
※ pip install pandas 명령을 실행했다.