pandas - 将两个数据帧列中的时间和日期合并到日期时间列

发布于 2025-01-16 07:02:42 字数 1360 浏览 0 评论 0原文

这是此处中已接受解决方案的后续问题。

我有一个 pandas 数据框:

在一列中,“时间”是以以下格式存储的时间:“HHMMSS”(例如 203412 表示 20:34:12)。

在另一列“日期”中,日期以以下格式存储:“YYmmdd”(例如,200712 表示 2020-07-12)。 YY 表示 2000 年的插件。

示例:

import pandas as pd

data = {'time': ['123455', '000010', '100000'],
        'date': ['200712', '210601', '190610']}

df = pd.DataFrame(data)

print(df)

#     time    date
#0  123455  200712
#1  000010  210601
#2  100000  190610

我需要第三列,其中包含以下日期时间的组合格式(例如 2020-07-12 12:34:55)另外两列。目前我只能修改时间,不知道如何添加日期。

df['datetime'] = pd.to_datetime(df['time'], format='%H%M%S')

print(df)

#     time    date            datetime
#0  123455  200712 1900-01-01 12:34:55
#1  000010  210601 1900-01-01 00:00:10
#2  100000  190610 1900-01-01 10:00:00

如何在 df['datetime'] 列中添加 df['date'] 列中的日期,以便数据框为:

     time    date            datetime
0  123455  200712 2020-07-12 12:34:55
1  000010  210601 2021-06-01 00:00:10
2  100000  190610 2019-06-10 10:00:00

我找到了这个 问题,但我不太确定如何使用它来达到我的目的。

This is a follow up question of the accepted solution in here.

I have a pandas dataframe:

In one column 'time' is the time stored in the following format: 'HHMMSS' (e.g. 203412 means 20:34:12).

In another column 'date' the date is stored in the following format: 'YYmmdd' (e.g 200712 means 2020-07-12). YY represents the addon to the year 2000.

Example:

import pandas as pd

data = {'time': ['123455', '000010', '100000'],
        'date': ['200712', '210601', '190610']}

df = pd.DataFrame(data)

print(df)

#     time    date
#0  123455  200712
#1  000010  210601
#2  100000  190610

I need a third column which contains the combined datetime format (e.g. 2020-07-12 12:34:55) of the two other columns. So far, I can only modify the time but I do not know how to add the date.

df['datetime'] = pd.to_datetime(df['time'], format='%H%M%S')

print(df)

#     time    date            datetime
#0  123455  200712 1900-01-01 12:34:55
#1  000010  210601 1900-01-01 00:00:10
#2  100000  190610 1900-01-01 10:00:00

How can I add in column df['datetime'] the date from column df['date'], so that the dataframe is:

     time    date            datetime
0  123455  200712 2020-07-12 12:34:55
1  000010  210601 2021-06-01 00:00:10
2  100000  190610 2019-06-10 10:00:00

I found this question, but I am not exactly sure how to use it for my purpose.

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

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

发布评论

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

评论(1

那支青花 2025-01-23 07:02:42

您可以先连接列,然后指定格式:

df['datetime'] = pd.to_datetime(df['date'] + df['time'], format='%y%m%d%H%M%S')
print(df)
     time    date            datetime
0  123455  200712 2020-07-12 12:34:55
1  000010  210601 2021-06-01 00:00:10
2  100000  190610 2019-06-10 10:00:00

如果可能的话整数列:

df['datetime'] = pd.to_datetime(df['date'].astype(str) + df['time'].astype(str), format='%y%m%d%H%M%S')

You can join columns first and then specify formar:

df['datetime'] = pd.to_datetime(df['date'] + df['time'], format='%y%m%d%H%M%S')
print(df)
     time    date            datetime
0  123455  200712 2020-07-12 12:34:55
1  000010  210601 2021-06-01 00:00:10
2  100000  190610 2019-06-10 10:00:00

If possible integer columns:

df['datetime'] = pd.to_datetime(df['date'].astype(str) + df['time'].astype(str), format='%y%m%d%H%M%S')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文