当前位置:网站首页>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)
边栏推荐
猜你喜欢
随机推荐
关于 DataFrame: 处理时间
Auto.js中的悬浮窗
单选点击可取消功能
美创科技勒索病毒“零信任”防护和数据安全治理体系的探索实践
Redis命令手册
Kerberos认证
快消品行业经销商协同系统:实现经销商可视化管理,提高沟通执行效率
ES6中的for...in/of的使用
Huawei router clock near the drainage experiment (using stream strategy)
kuberentes Auditing 入门
Likou 221 questions, the largest square
ArcPy读取Excel时序数据、批量反距离加权IDW插值与掩膜
ArcGIS自动随机生成采样点的方法
姜还是老的辣,看看老战哥的老底儿和严谨劲儿
Single-click to cancel the function
TortoiseSVN小乌龟的使用
RADIUS Authentication Server Deployment Costs That Administrators Must Know
工程师应该怎么学习
图数据库(Neo4j)入门
C. Social Distance