1 回答
TA贡献1797条经验 获得超6个赞
为了使用 datetime布尔选择,
log_time
需要将该列转换为 datetime 列,或者创建一个单独的 datetime 列log_time
。对于此数据,使用
pandas.to_datetime
并指定, 。unit='s'
import pandas as pd
# setup the dataframe
data = {'id': [154, 161, 76, 97, 76, 82, 154, 163, 161, 161],
'log_time': [1176369676, 1176369723, 1176373591, 1176381981, 1176415869, 1176421986, 1176421986, 1176421986, 1176421986, 1176437973],
'class': ['A', 'E', 'C', 'A', 'C', 'B', 'A', 'B', 'D', 'E']}
df = pd.DataFrame(data)
# create a datetime column from log_time
df['datetime'] = pd.to_datetime(df.log_time, unit='s')
# display(dflhead())
id log_time class datetime
0 154 1176369676 A 2007-04-12 09:21:16
1 161 1176369723 E 2007-04-12 09:22:03
2 76 1176373591 C 2007-04-12 10:26:31
3 97 1176381981 A 2007-04-12 12:46:21
4 76 1176415869 C 2007-04-12 22:11:09
# select data
selected = df[df.datetime > '2007-04-13']
# display(selected)
id log_time class datetime
9 161 1176437973 E 2007-04-13 04:19:33
添加回答
举报