pandas - 将两个数据帧列中的时间和日期合并到日期时间列
这是此处中已接受解决方案的后续问题。
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以先连接列,然后指定格式:
如果可能的话整数列:
You can join columns first and then specify formar:
If possible integer columns: