我有一个DataFrame,如下所示-
日期 | 时间 | url | 户名|
---|---|---|---|
2022-02-22 | 1900-01-01 00:00:02 | /test/fun.aspx | itesh|
2022-02-23 | 1900-01-01 01:00:02 | test/run.aspx||
2022-02-24 | 1900-01-01 01:00:02 | test/bun.aspxazim | >192.168.0.11 |
有很多方法,最有效的可能是将日期和时间连接到pandas.to_datetime
:
df['datetime'] = pd.to_datetime(df['date']+' '
+df['time'].str.extract('(S+)$',
expand=False)
)
输出:
date time url username ip datetime
0 2022-02-22 1900-01-01 00:00:02 /test/fun.aspx mitesh 192.168.0.25 2022-02-22 00:00:02
1 2022-02-23 1900-01-01 01:00:02 /test/run.aspx steve 192.168.0.15 2022-02-23 01:00:02
2 2022-02-24 1900-01-01 01:00:02 /test/bun.aspx azim 192.168.0.11 2022-02-24 01:00:02
提取时间的替代方案:
df['time'].str.split().str[-1]
完整日期操作备选方案:
pd.to_datetime(df['date']) + pd.to_datetime(df['time']).sub(pd.Timestamp('1900-01-01'))