Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

replace dataframe values based on another dataframe

I have a pandas dataframe which is structured as follows:

             timestamp       y
0 2020-01-01 00:00:00   336.0
1 2020-01-01 00:15:00   544.0
2 2020-01-01 00:30:00   736.0
3 2020-01-01 00:45:00   924.0
4 2020-01-01 01:00:00  1260.0
...

The timestamp column is a datetime data type

and I have another dataframe with the following structure:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

                     y
timestamp
00:00:00    625.076923
00:15:00    628.461538
00:30:00    557.692308
00:45:00    501.692308
01:00:00    494.615385
...

I this case, the time is the pandas datetime index.

Now what I want to do is replace the values in the first dataframe where the time field is matching i.e. the time of the day is matching with the second dataset.

>Solution :

IIUC your first dataframe df1’s timestamp is datetime type and your second dataframe (df2) has an index of type datetime as well but only time and not date.

then you can do:

df1['y'] = df1['timestamp'].dt.time.map(df2['y'])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading