■ DataFrame 클래스의 sort_values 메소드에서 inplace 인자를 사용해 특정 컬럼을 기준으로 정렬하는 방법을 보여준다.
▶ 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 |
import pandas as pd import numpy as np url = "https://raw.githubusercontent.com/pandas-dev/pandas/main/pandas/tests/io/data/csv/tips.csv" dataFrame = pd.read_csv(url) print(dataFrame) """ total_bill tip sex smoker day time size 0 16.99 1.01 Female No Sun Dinner 2 1 10.34 1.66 Male No Sun Dinner 3 2 21.01 3.50 Male No Sun Dinner 3 3 23.68 3.31 Male No Sun Dinner 2 4 24.59 3.61 Female No Sun Dinner 4 .. ... ... ... ... ... ... ... 239 29.03 5.92 Male No Sat Dinner 3 240 27.18 2.00 Female Yes Sat Dinner 2 241 22.67 2.00 Male Yes Sat Dinner 2 242 17.82 1.75 Male No Sat Dinner 2 243 18.78 3.00 Female No Thur Dinner 2 [244 rows x 7 columns] """ print() dataFrame.sort_values("tip", inplace = True) print(dataFrame) """ total_bill tip sex smoker day time size 92 5.75 1.00 Female Yes Fri Dinner 2 111 7.25 1.00 Female No Sat Dinner 1 67 3.07 1.00 Female Yes Sat Dinner 1 236 12.60 1.00 Male Yes Sat Dinner 2 0 16.99 1.01 Female No Sun Dinner 2 .. ... ... ... ... ... ... ... 141 34.30 6.70 Male No Thur Lunch 6 59 48.27 6.73 Male No Sat Dinner 4 23 39.42 7.58 Male No Sat Dinner 4 212 48.33 9.00 Male No Sat Dinner 4 170 50.81 10.00 Male Yes Sat Dinner 3 [244 rows x 7 columns] """ |
▶ 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 명령을 실행했다.