0

I'm trying to merge two dataframes as belows:

| Category | Date|

| --- | --- |

| A        | 2022-06-01 |

| A        | 2022-06-10 |

| A        | 2022-06-20 |

And the dataframe

| Category | Date      | Value |

| -------- | ----------|-------|

| A        | 2022-06-01| 100 |

| A        | 2022-06-12| 200 |

| A        | 2022-06-25| 300 |

And the result should be like this. Values of the result df should be based on the closest date of the first df.

| Category | Date| Value |

| -------- | -------------- | --------| 

| A        | 2022-06-01     | 100     |

| A        | 2022-06-10     | 200     |

| A        | 2022-06-20     | 300     |

Can someone help?

anewone
  • 27
  • 4
  • how are you getting the value for the second and third rows? is it based on the value for the closest date? – Emi OB Jun 09 '22 at 11:29
  • What dictates the `Value` column in your expected output? How does the row which was for `2022-06-10` get the value `200`? Is it because they have the same number of rows, or does it depend on the nearest date or something? – Steve Jun 09 '22 at 11:31
  • Yes, it's based on the closest date of the first df. – anewone Jun 09 '22 at 11:31
  • Sounds like [Pandas Merge on Name and Closest Date](https://stackoverflow.com/questions/24614474/pandas-merge-on-name-and-closest-date) might help – Steve Jun 09 '22 at 11:33
  • You might need to make sure your `Date` column is interpreted as a date object (depends how you're loading your dataframe in) – Steve Jun 09 '22 at 11:35

0 Answers0