1

I have got 2 dataframes: d1 columns: ['Text', 'Date', 'Date_with_affect'] - with Id as index:

89, 'Hello', 2018-03-05, 2018-03-06

d2 columns: ['Price', 'Open', 'High', 'Low', 'Vol.', 'Change'] - with Date set as index

2018-03-06, 12, 13, 14, 11, 0, 0.5

I would like to add new column to the d1 dataframe that will be a 'Change' column from d2 by "Date_with_affect" matching the "Date" from d2. So the result would be:

89, 'Hello', 2018-03-05, 2018-03-06, 0.5

And if date is not in d2 then should pick the next day. What is the easiest way to that? I tried to do that but I guess that it is completly wrong.

d1["Change"] = d2[d1["Date_with_affect"]].Change
1
  • 1
    could you paste the output of d1.head() and d2.head() to illustrate the data contained in the two dataframes Commented Mar 8, 2018 at 15:34

1 Answer 1

4

For this case I think you can use merge_asof (also, I assuming your date is datetime format. If not, try to converting it using pd.to_datetime):

pd.merge_asof(
   d1,
   d2.reset_index()[['Date','Change']],
   left_on='Date_with_affect',
   right_on='Date'
)
Sign up to request clarification or add additional context in comments.

3 Comments

All about that aesthetic.
I get error: "ValueError: left keys must be sorted".Both dates are datatimes
@AlaGłowacka using sort_values before merge

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.