■ to_datetime 함수를 사용해 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import pandas as pd dataFrame = pd.read_csv("https://s3-us-west-2.amazonaws.com/streamlit-demo-data/uber-raw-data-sep14.csv.gz", nrows = 1000) print("[변경전]") print(dataFrame) print() dataTimeSeries1 = dataFrame["date/time"] dataTimeSeries2 = pd.to_datetime(dataTimeSeries1) print("[변경후]") print(dataFrame) print() """ [변경전] Date/Time Lat Lon Base 0 9/1/2014 0:01:00 40.2201 -74.0021 B02512 1 9/1/2014 0:01:00 40.7500 -74.0027 B02512 2 9/1/2014 0:03:00 40.7559 -73.9864 B02512 3 9/1/2014 0:06:00 40.7450 -73.9889 B02512 4 9/1/2014 0:11:00 40.8145 -73.9444 B02512 .. ... ... ... ... 995 9/2/2014 11:11:00 40.7381 -73.9878 B02512 996 9/2/2014 11:14:00 40.7848 -73.9560 B02512 997 9/2/2014 11:14:00 40.7848 -73.9560 B02512 998 9/2/2014 11:17:00 40.7741 -73.9608 B02512 999 9/2/2014 11:18:00 40.7410 -73.7579 B02512 [1000 rows x 4 columns] [변경후] Date/Time Lat Lon Base 0 2014-09-01 00:01:00 40.2201 -74.0021 B02512 1 2014-09-01 00:01:00 40.7500 -74.0027 B02512 2 2014-09-01 00:03:00 40.7559 -73.9864 B02512 3 2014-09-01 00:06:00 40.7450 -73.9889 B02512 4 2014-09-01 00:11:00 40.8145 -73.9444 B02512 .. ... ... ... ... 995 2014-09-02 11:11:00 40.7381 -73.9878 B02512 996 2014-09-02 11:14:00 40.7848 -73.9560 B02512 997 2014-09-02 11:14:00 40.7848 -73.9560 B02512 998 2014-09-02 11:17:00 40.7741 -73.9608 B02512 999 2014-09-02 11:18:00 40.7410 -73.7579 B02512 [1000 rows x 4 columns] """ |
▶ requirements.txt
1 2 3 4 5 6 7 8 |
numpy==2.0.0 pandas==2.2.2 python-dateutil==2.9.0.post0 pytz==2024.1 six==1.16.0 tzdata==2024.1 |
※ pip install pandas 명령을 실행했다.