■ Series 클래스의 iloc 속성과 [] 연산자를 사용해 슬라이싱을 처리하는 방법을 보여준다.
▶ 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import numpy as np import pandas as pd series1 = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) print(series1) print("-" * 50) """ a 0.794678 b 1.756467 c -0.014899 d 0.926113 e -1.238260 dtype: float64 """ value1 = series1.iloc[0] print(value1) # numpy.float64 print("-" * 50) """ 0.7946776583297425 """ series2 = series1.iloc[:3] print(series2) print("-" * 50) """ a 0.794678 b 1.756467 c -0.014899 dtype: float64 """ medianValue = series1.median() print(medianValue) print("-" * 50) """ 0.7946776583297425 """ series3 = series1[series1 > medianValue] print(series3) print("-" * 50) """ b 1.756467 d 0.926113 dtype: float64 """ series4 = series1.iloc[[4, 3, 1]] print(series4) print("-" * 50) """ e -1.238260 d 0.926113 b 1.756467 dtype: float64 """ series5 = np.exp(series1) # e^x 연산을 실행한다. print(series5) print("-" * 50) """ a 2.213727 b 5.791940 c 0.985212 d 2.524677 e 0.289888 dtype: float64 """ |
▶ requirements.txt
1 2 3 4 5 6 7 8 |
numpy==2.1.3 pandas==2.2.3 python-dateutil==2.9.0.post0 pytz==2024.2 six==1.17.0 tzdata==2024.2 |
※ pip install pandas 명령을 실행했다.