Pandas-如何从Python的datetime列中提取HH:MM? [英] Pandas - How to extract HH:MM from datetime column in Python?
本文介绍了Pandas-如何从Python的datetime列中提取HH:MM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想从我的df
HH:MM中提取.我该怎么办?
I just want to extract from my df
HH:MM. How do I do it?
这是df
中该列的描述:
count 810
unique 691
top 2018-07-25 11:14:00
freq 5
Name: datetime, dtype: object
字符串值包含完整的时间戳记.目标是将每一行的HH:MM
解析到另一个df中,然后循环回而仅将%Y-%m-%d
提取到另一个df中.
The string value includes a full time stamp. The goal is to parse each row's HH:MM
into another df, and to loop back over and extract just the %Y-%m-%d
into another df.
推荐答案
假设df看起来像
print(df)
date_col
0 2018-07-25 11:14:00
1 2018-08-26 11:15:00
2 2018-07-29 11:17:00
#convert from string to datetime
df['date_col'] = pd.to_datetime(df['date_col'])
#to get date only
print(df['date_col'].dt.date)
0 2018-07-25
1 2018-08-26
2 2018-07-29
#to get time:
print(df['date_col'].dt.time)
0 11:14:00
1 11:15:00
2 11:17:00
#to get hour and minute
print(df['date_col'].dt.strftime('%H:%M'))
0 11:14
1 11:15
2 11:17
这篇关于Pandas-如何从Python的datetime列中提取HH:MM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文