Pandas 中文日期 年月日处理成datetimepython
须要将中文字符串日期转换成 datetime处理;微信
示例:

补充代码以下: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 |

使用print断点调试3d

结果所示:

参考
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