■ DatetimeProperties 클래스의 year/month/day 속성을 사용해 DataFrame 객체 컬럼의 값을 설정하는 방법을 보여준다.
▶ 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.read_excel("tips.xlsx", index_col = 0) dataFrame["date1"] = pd.Timestamp("2013-01-15") datetimeProperties = dataFrame["date1"].dt dataFrame["date1_year" ] = datetimeProperties.year dataFrame["date1_month"] = datetimeProperties.month dataFrame["date1_day" ] = datetimeProperties.day print(dataFrame) """ total_bill tip sex smoker day time size date1 date1_year date1_month date1_day 0 16.99 1.01 Female No Sun Dinner 2 2013-01-15 2013 1 15 1 10.34 1.66 Male No Sun Dinner 3 2013-01-15 2013 1 15 2 21.01 3.50 Male No Sun Dinner 3 2013-01-15 2013 1 15 3 23.68 3.31 Male No Sun Dinner 2 2013-01-15 2013 1 15 4 24.59 3.61 Female No Sun Dinner 4 2013-01-15 2013 1 15 .. ... ... ... ... ... ... ... ... ... ... ... 239 29.03 5.92 Male No Sat Dinner 3 2013-01-15 2013 1 15 240 27.18 2.00 Female Yes Sat Dinner 2 2013-01-15 2013 1 15 241 22.67 2.00 Male Yes Sat Dinner 2 2013-01-15 2013 1 15 242 17.82 1.75 Male No Sat Dinner 2 2013-01-15 2013 1 15 243 18.78 3.00 Female No Thur Dinner 2 2013-01-15 2013 1 15 [244 rows x 11 columns] """ |
▶ requirements.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
defusedxml==0.7.1 et_xmlfile==2.0.0 numpy==2.1.3 odfpy==1.4.1 openpyxl==3.1.5 pandas==2.2.3 python-calamine==0.2.3 python-dateutil==2.9.0.post0 pytz==2024.2 pyxlsb==1.0.10 six==1.16.0 tzdata==2024.2 xlrd==2.0.1 XlsxWriter==3.2.0 |
※ pip install “pandas[excel]” 명령을 실행했다.