■ CategoricalAccessor 클래스의 set_categories 메소드를 사용해 카테고리 리스트를 설정하는 방법을 보여준다.
▶ 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 |
import pandas as pd dataFrame = pd.DataFrame( { "id" : [1, 2, 3, 4, 5, 6], "raw_grade" : ["a", "b", "b", "a", "a", "e"] } ) dataFrame["grade"] = dataFrame["raw_grade"].astype("category") newCategoryList = ["very good", "good", "very bad"] series1 = dataFrame["grade"] categoricalAccessor1 = series1.cat dataFrame["grade"] = categoricalAccessor1.rename_categories(newCategoryList) series2 = dataFrame["grade"] categoricalAccessor2 = series2.cat dataFrame["grade"] = categoricalAccessor2.set_categories(["very bad", "bad", "medium", "good", "very good"]) print(dataFrame["grade"]) """ 0 very good 1 good 2 good 3 very good 4 very good 5 very bad Name: grade, dtype: category Categories (5, object): ['very bad', 'bad', 'medium', 'good', 'very good'] """ |
▶ 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 명령을 실행했다.