Pandas 中文日期 年月日處理成datetimepython
須要將中文字符串日期轉換成 datetime處理;微信
示例:
![image-20201118151026814](http://static.javashuo.com/static/loading.gif)
補充代碼以下:app
def date_parse(x):
x = x.str.split('月份').str[0].add('月01日')
x = x.str.replace('年', '-').str.replace('月', '-').str.replace('日', '')
x = pd.to_datetime(x, format='%Y-%m', errors='coerce').dt.date
return x
df[['month_dt']] = df[['月份']].apply(date_parse)
df
代碼詳解:spa
創新變例子:另一個示例詳解更好理解:
這是另一個示例:.net
time | 當月 | |
---|---|---|
0 | 2019年6月27日10時 | 100.5 |
1 | 2019年8月28日10時 | 101.7 |
2 | 2019年8月5日10時30分 | 102.4 |
3 | 2019年9月3日10時 | 102.7 |
4 | 2019年9月3日10時 | 102.5 |
5 | 2019年8月5日10時 | 102.4 |
![image-20201118151314577](http://static.javashuo.com/static/loading.gif)
使用print斷點調試3d
![image-20201118151404953](http://static.javashuo.com/static/loading.gif)
結果所示:
![image-20201118151434632](http://static.javashuo.com/static/loading.gif)
參考
pandas - Extract date from chinese characters string date in Python - Stack Overflow]調試
(9 封私信 / 3 條消息) pandas dataframe如何識別中文日期? - 知乎]code
本文分享自微信公衆號 - SQL數據分析(dianwu_dw)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。orm