給定以下df字串date列,其中日的序號、月份的縮寫月份名稱和正常年份:
date oil gas
0 1st Oct 2021 428 99
1 10th Sep 2021 401 101
2 2nd Oct 2020 189 74
3 10th Jan 2020 659 119
4 1st Nov 2019 691 130
5 30th Aug 2019 742 162
6 10th May 2019 805 183
7 24th Aug 2018 860 182
8 1st Sep 2017 759 183
9 10th Mar 2017 617 151
10 10th Feb 2017 591 149
11 22nd Apr 2016 343 88
12 10th Apr 2015 760 225
13 23rd Jan 2015 1317 316
我想知道我們如何將date列決議為標準%Y-%m-%d格式?
到目前為止我的想法: 1.'st', 'nd', 'rd', 'th'從字符日期字串中去除序數指示符 (),同時將日期編號保留為re; 2. 并將縮寫的月份名稱轉換為數字(似乎不是%b),3. 最后將它們轉換為%Y-%m-%d.
代碼可能對第一步有用:
re.compile(r"(?<=\d)(st|nd|rd|th)").sub("", df['date'])
參考:
https://metacpan.org/release/DROLSKY/DateTime-Locale-0.46/view/lib/DateTime/Locale/en_US.pm#Months
uj5u.com熱心網友回復:
pd.to_datetime如果您不指定format引數,則已經處理了這種情況:
>>> pd.to_datetime(df['date'])
0 2021-10-01
1 2021-09-10
2 2020-10-02
3 2020-01-10
4 2019-11-01
5 2019-08-30
6 2019-05-10
7 2018-08-24
8 2017-09-01
9 2017-03-10
10 2017-02-10
11 2016-04-22
12 2015-04-10
13 2015-01-23
Name: date, dtype: datetime64[ns]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442401.html
標籤:python-3.x 熊猫 约会时间 python-dateutil
下一篇:將列轉換為日期格式
