将带有额外数字的时间列转换为全职列,为小时:分钟:SECS

发布于 2025-01-21 14:45:12 字数 523 浏览 0 评论 0原文

我正在研究时间序列数据框架。DF如下:

0   2019-01-01  Contact    Tuesday  False   January 04:00:00.118000 1
1   2019-01-01  Contact    Tuesday  False   January 04:00:00.483000 1
2   2019-01-01  Contact    Tuesday  False   January 08:00:00.162000 1
3   2019-01-01  Contact    Tuesday  False   January 08:00:00.426000 1
4   2019-01-01  Contact    Tuesday  False   January 08:00:00.564000 1

要获得此DF,我在上方进行了其他转换,因此这不是直接负载。 因此,我试图用04:00:00.118000将第二列转换为04:00:00。 实现这一目标的最快方法是什么?

I am working on a time series data frame.The df is as follows:

0   2019-01-01  Contact    Tuesday  False   January 04:00:00.118000 1
1   2019-01-01  Contact    Tuesday  False   January 04:00:00.483000 1
2   2019-01-01  Contact    Tuesday  False   January 08:00:00.162000 1
3   2019-01-01  Contact    Tuesday  False   January 08:00:00.426000 1
4   2019-01-01  Contact    Tuesday  False   January 08:00:00.564000 1

To get this df I have done other transformation above hence, this is not a direct load.
so I am trying to convert the second last column with 04:00:00.118000 to 04:00:00.
What is the quickest way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅的被狗咬 2025-01-28 14:45:13

如果您的第二列中的条目是类型dateTime.Time,则可以使用以下内容:

df[name] = df[name].apply(lambda t: t.replace(microsecond=0))

其中name是第二列到最后一列的名称。如果它们是类型str,则可以使用此方法:

df[name] = df[name].apply(lambda t: t.split('.')[0])

If your entries in the second to last column are of type datetime.time, you could use the following:

df[name] = df[name].apply(lambda t: t.replace(microsecond=0))

where name is the name of your second to last column. If they are of type str, then you could use this instead:

df[name] = df[name].apply(lambda t: t.split('.')[0])
缪败 2025-01-28 14:45:13

,则应该起作用

尝试一下,如果您有对象类型数据

>>> df
                      date  col1
0  January 04:00:00.118000     1
1  January 04:00:00.483000     1
2  January 08:00:00.162000     1
3  January 08:00:00.426000     1
>>> df.dtypes
date    object
col1     int64
dtype: object

>>> df['date'] = df['date'].str.split(".").str[0]
>>> df
               date  col1
0  January 04:00:00     1
1  January 04:00:00     1
2  January 08:00:00     1
3  January 08:00:00     1

Try this, if you have the Object type data then it should work..

Sample data mimicking the data ..

>>> df
                      date  col1
0  January 04:00:00.118000     1
1  January 04:00:00.483000     1
2  January 08:00:00.162000     1
3  January 08:00:00.426000     1
>>> df.dtypes
date    object
col1     int64
dtype: object

Solution

>>> df['date'] = df['date'].str.split(".").str[0]
>>> df
               date  col1
0  January 04:00:00     1
1  January 04:00:00     1
2  January 08:00:00     1
3  January 08:00:00     1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文