Home »
Python »
Python Programs
Filter/Select rows of pandas dataframe by timestamp column
Learn, how to filter/select rows of pandas dataframe by timestamp column in Python?
By Pranit Sharma Last updated : September 19, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
The timestamp value is the value that contains the date and time values in a particular format. It comes from the Datetime library. If we use pd.Timestamp() method and pass a string inside it, it will convert this string into time format.
Problem statement
Suppose we are given a dataframe with 2 columns out of which one column is a timestamp column and the other column contains some numerical values. We need to find an easy way to create a new data frame from the original data frame that contains rows between the two timestamp values, it means we are required to filter the rows of the original data frame in a specific range of dates.
Selecting rows of pandas dataframe by timestamp column
For this purpose, we will first convert the timestamp column into DateTime and then we will apply a condition in which we will define a specific range within which we will extract all the values.
Let us understand with the help of an example,
Python code to filter/select rows of pandas dataframe by timestamp column
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating a dictionary
d = {
'dt':['7/23/2014','4/13/2014','8/30/2014','8/12/2014',
'1/1/2014','9/02/2014','12/13/2014','12/1/2014'],
'value':[1,2,3,4,5,6,7,8]
}
# Creating a DataFrame
df = pd.DataFrame(d)
# Display Original df
print("Original DataFrame:\n",df,"\n")
# Converting dt into datetime
df['dt'] = pd.to_datetime(df['dt'])
# Selecting rows between two dates
res = df[(df['dt'] > '2014-07-23 07:30:00') & (df['dt'] < '2014-12-13 09:00:00')]
# Display result
print("Result:\n",res)
Output
The output of the above program is:
Python Pandas Programs »