
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Compare specific Timestamps for a Pandas DataFrame – Python
To compare specific timestamps, use the index number in the square brackets. At first, import the required library −
import pandas as pd
Create a DataFrame with 3 columns. We have two date columns with timestamp −
dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"], "Date_of_Purchase": [ pd.Timestamp("2021-06-10"), pd.Timestamp("2021-07-11"), pd.Timestamp("2021-06-25"), pd.Timestamp("2021-06-29"), pd.Timestamp("2021-03-20"), ], "Date_of_Service": [ pd.Timestamp("2021-11-05"), pd.Timestamp("2021-12-03"), pd.Timestamp("2021-10-30"), pd.Timestamp("2021-11-29"), pd.Timestamp("2021-08-20"), ] })
Find specific Timestamps, let’s say 1 to 3 rows −
timestamp1_diff = abs(dataFrame['Date_of_Purchase'][0]-dataFrame['Date_of_Service'][0]) timestamp2_diff = abs(dataFrame['Date_of_Purchase'][1]-dataFrame['Date_of_Service'][1]) timestamp3_diff = abs(dataFrame['Date_of_Purchase'][2]-dataFrame['Date_of_Service'][2])
Example
Following is the code −
import pandas as pd # create a dataframe with 3 columns dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"], "Date_of_Purchase": [ pd.Timestamp("2021-06-10"), pd.Timestamp("2021-07-11"), pd.Timestamp("2021-06-25"), pd.Timestamp("2021-06-29"), pd.Timestamp("2021-03-20"), ], "Date_of_Service": [ pd.Timestamp("2021-11-05"), pd.Timestamp("2021-12-03"), pd.Timestamp("2021-10-30"), pd.Timestamp("2021-11-29"), pd.Timestamp("2021-08-20"), ] }) print"DataFrame...\n", dataFrame # compare specific timestamps timestamp1_diff = abs(dataFrame['Date_of_Purchase'][0]-dataFrame['Date_of_Service'][0]) timestamp2_diff = abs(dataFrame['Date_of_Purchase'][1]-dataFrame['Date_of_Service'][1]) timestamp3_diff = abs(dataFrame['Date_of_Purchase'][2]-dataFrame['Date_of_Service'][2]) print"\nDifference between Car 1 Date of Purchase and Service \n",timestamp1_diff print"\nDifference between Car 2 Date of Purchase and Service \n",timestamp2_diff print"\nDifference between Car 3 Date of Purchase and Service \n",timestamp3_diff
Output
This will produce the following output −
DataFrame... Car Date_of_Purchase Date_of_Service 0 Audi 2021-06-10 2021-11-05 1 Lexus 2021-07-11 2021-12-03 2 Tesla 2021-06-25 2021-10-30 3 Mercedes 2021-06-29 2021-11-29 4 BMW 2021-03-20 2021-08-20 Difference between Car 1 Date of Purchase and Service 148 days 00:00:00 Difference between Car 2 Date of Purchase and Service 145 days 00:00:00 Difference between Car 3 Date of Purchase and Service 127 days 00:00:00
- Related Articles
- Python - Search DataFrame for a specific value with pandas
- Python - Sum only specific rows of a Pandas Dataframe
- Python - Drop specific rows from multiindex Pandas Dataframe
- Python Pandas - Display specific number of rows from a DataFrame
- Finding difference in Timestamps – Python Pandas
- Python - Draw a Scatter Plot for a Pandas DataFrame
- Python - Plot a Histogram for Pandas Dataframe with Matplotlib?
- Python - Display True for infinite values in a Pandas DataFrame
- Python - Plot a Pie Chart for Pandas Dataframe with Matplotlib?
- How to get the sum of a specific column of a dataframe in Pandas Python?
- Python - Density Plots with Pandas for a specific attribute
- How to find the standard deviation of specific columns in a dataframe in Pandas Python?
- How to compare timestamps in MySQL?
- Python – Strip whitespace from a Pandas DataFrame
- Python Pandas - Create a time interval and use Timestamps as the bounds

Advertisements