当前位置:网站首页>关于 DataFrame: 处理时间
关于 DataFrame: 处理时间
2022-08-10 21:05:00 【大地之灯】
目录
主要是将自己遇到的处理时间的问题加以总结,会一直更新的
字符串转时间
时间切分两种方式,xx.str.slice() 或者 用正则表达式去提取
提取时间
实例:
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)
边栏推荐
- paddle 35 paddledetection保存训练过程中的log信息
- In 2021 China industrial Internet security competition (competition) in fujian province and the first industry of fujian province Internet innovation competition
- UPDATE:修改数据语法使用例——《mysql 从入门到内卷再到入土》
- Single-click to cancel the function
- [Golang]用反射让你的代码变优美
- 【PCBA solution】Electronic grip strength tester solution she'ji
- 【网络通信四】ping工具源码cmake工程编译以及运行说明
- 快消品行业经销商协同系统:实现经销商可视化管理,提高沟通执行效率
- INSERT:插入操作语法&使用例——《mysql 从入门到内卷再到入土》
- 优化是一种习惯●出发点是'站在靠近临界'的地方
猜你喜欢

TortoiseSVN小乌龟的使用

apr_thread使用内存之谜

Live Classroom System 08-Tencent Cloud Object Storage and Course Classification Management

姜还是老的辣,看看老战哥的老底儿和严谨劲儿

【一致性hash】负载均衡器分发请求

组合导航精度分析

【PCBA solution】Electronic grip strength tester solution she'ji

【Windows】你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问,这些策略可帮助保护你的电脑

paddle 35 paddledetection保存训练过程中的log信息

ES6中的for...in/of的使用
随机推荐
DELETE:删除操作语法&使用例——《mysql 从入门到内卷再到入土》
Live Classroom System 09--Tencent Cloud VOD Management Module (1)
apr_thread使用内存之谜
DDL:ALTER 修改数据库——《mysql 从入门到内卷再到入土》
【Windows】你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问,这些策略可帮助保护你的电脑
ArcGIS自动随机生成采样点的方法
Application of Spatial 3D Model Reconstruction Based on Pix4Dmapper - Spatial Analysis and Site Selection
变量和它的特性——《mysql 从入门到内卷再到入土》
LeetCode-498-对角线遍历
MATLAB神经网络拟合工具箱Neural Net Fitting使用方法
睡前故事|用Bitmap与AST做一个配置化时长系统
ctfshow-osint
leetcode:45. 跳跃游戏II
数字化转型:如何引导创新领导者
每次打开chrome会跳出What's new
C. Rotation Matching
地理探测器Geodetector软件的下载、应用与结果解读
饿了么-机构树单选
TortoiseSVN小乌龟的使用
ACM解题笔记——HDU 1401 Solitaire(DBFS)