简体   繁体   中英

pandas: filter data using column in unix timestamp

One column of my dataframe contains unix timestamp. I am looking for a way to filter records by date similar to this SQL statement:

SELECT * FROM mytable WHERE to_timestamp(log_time) < '2007-04-13';

to filter records in my dataframe . Sample record in dataframe shown below where log_time is between 2007-04-12 and 2007-04-13 :

df.head(10)
id  log_time    class
154 1176369676  A
161 1176369723  E
76  1176373591  C
97  1176381981  A
76  1176415869  C
82  1176421986  B 
154 1176421986  A
163 1176421986  B
161 1176421986  D
161 1176437973  E
  • In order to use datetime Boolean selection , the log_time column needs to be converted to a datetime column, or create a separate datetime column from log_time .
  • Use pandas.to_datetime and specify unit='s' , for this data.
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM