如何将带有日期时间的DataFrame列拆分为两列:一列带日期,另一列带一天中的时间? [英] How can I split a DataFrame column with datetimes into two columns: one with dates and one with times of the day?
本文介绍了如何将带有日期时间的DataFrame列拆分为两列:一列带日期,另一列带一天中的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为data
的数据框,该数据框具有这样的列Dates
,
I have a data frame called data
, which has a column Dates
like this,
Dates
0 2015-05-13 23:53:00
1 2015-05-13 23:53:00
2 2015-05-13 23:33:00
3 2015-05-13 23:30:00
4 2015-05-13 23:30:00
我知道如何在数据框中添加一列,但是如何将Dates
划分为
I know how to add a column to data frame, but how to divide Dates
to
Day Time
0 2015-05-13 23:53:00
1 2015-05-13 23:53:00
2 2015-05-13 23:33:00
3 2015-05-13 23:30:00
4 2015-05-13 23:30:00
推荐答案
如果您的系列是s
,则将创建这样的DataFrame:
If your series is s
, then this will create such a DataFrame:
pd.DataFrame({
'date': pd.to_datetime(s).dt.date,
'time': pd.to_datetime(s).dt.time})
使用 pd.to_datetime
,则dt
成员可用于提取零件.
as once you convert the series using pd.to_datetime
, then the dt
member can be used to extract the parts.
示例
import pandas as pd
s = pd.Series(['2015-05-13 23:53:00', '2015-05-13 23:53:00'])
>>> pd.DataFrame({
'date': pd.to_datetime(s).dt.date,
'time': pd.to_datetime(s).dt.time})
date time
0 2015-05-13 23:53:00
1 2015-05-13 23:53:00
这篇关于如何将带有日期时间的DataFrame列拆分为两列:一列带日期,另一列带一天中的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文