I have two Pandas Dataframes:
First:
Timestamp | Label | Value | Another col 1 | another col 2 |
---|---|---|---|---|
2022-01-01 00:00 | A | 0 | ... | ... |
2022-01-01 00:00 | B | 0 | ... | ... |
2022-01-01 00:15 | B | 0 | ... | ... |
2022-01-01 00:30 | B | 0 | ... | ... |
2022-01-01 00:45 | B | 0 | ... | ... |
2022-01-01 01:00 | C | 0 | ... | ... |
2022-01-01 01:00 | D | 0 | ... | ... |
2022-01-01 01:15 | D | 0 | ... | ... |
... | ... | ... | ... | ... |
Second:
Timestamp | Label | Value | Unrelated column A | Unrelated column B |
---|---|---|---|---|
2022-01-01 00:00 | A | 20 | ... | ... |
2022-01-01 01:00 | C | 20 | ... | ... |
... | ... | ... | ... | ... |
What I would like to do is the following:
- Take all rows from the
Second
dataframe - Find the matching row (by
Timestamp
andLabel
) in theFirst
dataframe (while also ignoring rows for which I can not find the match) - Update the
First.Value
column with the values fromSecond.Value
column
The final result would be updated First
dataframe:
Timestamp | Label | Value | Another col 1 | another col 2 |
---|---|---|---|---|
2022-01-01 00:00 | A | 20 | ... | ... |
2022-01-01 00:00 | B | 0 | ... | ... |
2022-01-01 00:15 | B | 0 | ... | ... |
2022-01-01 00:30 | B | 0 | ... | ... |
2022-01-01 00:45 | B | 0 | ... | ... |
2022-01-01 01:00 | C | 20 | ... | ... |
2022-01-01 01:00 | D | 0 | ... | ... |
2022-01-01 01:15 | D | 0 | ... | ... |
... | ... | ... | ... | ... |
In SQL world I could do something like update with join. I have no idea how to do it in Pandas. I have looked at DataFrame.update and DataFrame.merge but they all seem to be oriented towards completely merging the two dataframes whereas I just want to copy certain values. It seems it should probably be possible with some sort of multi-index but it is way beyond my knowledge.
Any help is appreciated
Edit: I have been asked if this is a duplicate of Pandas Merging 101. I do not see how it is, I need to update the first DataSet inplace and this does not seem to be possible with merge
.