当前位置:网站首页>About DataFrame: Processing Time
About DataFrame: Processing Time
2022-08-10 21:32:00 【Light of the Earth】
目录
The main thing is to summarize the processing time problems that you have encountered,会一直更新的
字符串转时间
Time split in two ways,xx.str.slice() 或者 Extract with regular expressions
提取时间
实例:
re.findall(r'(.*)T.*','2022-05-12T07:56:13')[0]
# '2022-05-12'
方法一:df_data[‘year’] = pd.to_datetime(df_data[‘date’]).dt.year
# 转换为时间类型
df["date"] = pd.to_datetime(df["date"], format='%Y-%m-%d')
# 获取年
df["year"] = pd.to_datetime(df["date"]).dt.year
# 获取月
df["month"] = pd.to_datetime(df["date"]).dt.month
# 获取日
df["day"] = pd.to_datetime(df["date"]).dt.day
# 获取周
df["week"] = pd.to_datetime(df["date"]).dt.week
print(df)
print(df.dtypes)
结果:
date year month day week
0 2019-12-09 2019 12 9 50
1 2019-12-02 2019 12 2 49
date datetime64[ns]
year int64
month int64
day int64
week int64
dtype: object
方法二:df_data[‘year’] = df_data[‘date’].apply(lambda x:x.strftime(“%Y”))
# 转换为时间
df["date"] = pd.to_datetime(df["date"])
# 获取年月日
df["year-month-day"] = df["date"].apply(lambda x: x.strftime("%Y-%m-%d"))
# 获取年
df["year"] = df["date"].apply(lambda x: x.strftime("%Y"))
# 获取月
df["month"] = df["date"].apply(lambda x: x.strftime("%m"))
# 获取日
df["day"] = df["date"].apply(lambda x: x.strftime("%d"))
# 获取月日
df["month-day"] = df["date"].apply(lambda x: x.strftime("%Y-%m"))
# 获取周
df['week'] = df['date'].apply(lambda x: x.strftime('%W'))
print(df)
print(df.dtypes)
边栏推荐
猜你喜欢

Detailed explanation and use of each module of ansible

直播课堂系统08补-腾讯云对象存储和课程分类管理

Redis 性能影响 - 异步机制和响应延迟

图扑智慧电力可视化大屏,赋能虚拟电厂精准减碳

LeetCode-498-对角线遍历

【PCBA方案】电子握力测试仪方案she‘ji

【PCBA scheme design】Bluetooth skipping scheme

【vulhub】MySql身份认证绕过漏洞复现(CVE-2012-2122)

Single-click to cancel the function

LeetCode-36-二叉搜索树与双向链表
随机推荐
Mark!画出漂亮的神经网络图!神经网络可视化工具集锦搜集
Auto.js中APP应用相关指令
Labelme-5.0.1 version edit polygon crash
直播课堂系统08-腾讯云对象存储和课程分类管理
D. Game With Array
wget编译升级故障解决
Date picker component (restrict year to set only displayed months)
Future-oriented IT infrastructure management architecture - Unified IaaS
如何提交一个PR?【OpenHarmony成长计划】【OpenHarmony开源社区】
2021DozerCTF
将视图模型转换为使用 Hilt 依赖注入
2021 GKCTF X DASCTF应急挑战杯
JS中的filter、map、reduce
地理探测器Geodetector软件的下载、应用与结果解读
C. Even Picture
饿了么-机构树单选
DDL:视图——《mysql 从入门到内卷再到入土》
用汇编带你看Golang里到底有没有值类型、引用类型
labelme-5.0.1版本编辑多边形闪退
ArcGIS自动随机生成采样点的方法