Pandas-从其他TimeStamp列生成日期列



我有一个DataFrame,如下所示-

户名iteshtest/run.aspxtest/bun.aspx>
日期 时间 url
2022-02-22 1900-01-01 00:00:02 /test/fun.aspx
2022-02-23 1900-01-01 01:00:02
2022-02-24 1900-01-01 01:00:02azim192.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'))

最新更新