Pandas How To Uncategorized How to join two dataframes on timestamp

How to join two dataframes on timestamp

To join two dataframes on timestamp, you need to have a common column in both dataframes that contains timestamps. Once you have identified the common column, you can use the pd.merge() function from the Pandas library to join the dataframes based on the timestamps. Here’s an example:

Suppose you have two dataframes: df1 and df2, both with a column called ‘timestamp’:

import pandas as pd

df1 = pd.DataFrame({'timestamp': ['2022-01-01 00:00:00', '2022-01-02 00:00:00', '2022-01-03 00:00:00'], 'value1': [1, 2, 3]})
df2 = pd.DataFrame({'timestamp': ['2022-01-01 00:00:00', '2022-01-03 00:00:00', '2022-01-04 00:00:00'], 'value2': [4, 5, 6]})

In this example, df1 and df2 both have a column called ‘timestamp’, which contains timestamps in the format ‘YYYY-MM-DD HH:MM:SS’.

To join the two dataframes based on the timestamp column, you can use the pd.merge() function as follows:

merged_df = pd.merge(df1, df2, on='timestamp')

This will create a new dataframe called merged_df, which contains the columns from df1 and df2 and rows where the timestamps match in both dataframes. The resulting dataframe will look like:

           timestamp  value1  value2
0  2022-01-01 00:00:00       1       4
1  2022-01-03 00:00:00       3       5

Note that the resulting dataframe only contains the rows where the timestamps match in both dataframes, and the columns from both dataframes are included in the merged dataframe. If you want to include all timestamps from both dataframes, you can use the how parameter in the pd.merge() function.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post