■ CategoricalAccessor 클래스의 rename_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 |
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"] series = dataFrame["grade"] categoricalAccessor = series.cat dataFrame["grade"] = categoricalAccessor.rename_categories(newCategoryList) print(dataFrame) """ id raw_grade grade 0 1 a very good 1 2 b good 2 3 b good 3 4 a very good 4 5 a very good 5 6 e very bad """ |
▶ 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 명령을 실행했다.