1

I have one dataframe in the form (with some other columns which aren't very relevant to the question)-

Max Date Stock
28-09-2026 A
22-03-2024 B
17-10-2023 C
17-05-2025 D

I have another dataframe with only a list of dates. For each date in the 2nd dataframe, I want to create a repeat of each stock and each "max date" from 1st dataframe in order to compare against that date and assign a classification based on whether the date is earlier or later.

Output should be like -

Date (2nd df) Stock Max Date Classification
01-02-2023 A 28-09-2026 Lower
10-03-2025 A 28-09-2026 Lower
01-02-2023 B 22-03-2024 Lower
10-03-2025 B 22-03-2024 Higher

I tried using pd.merge() after bringing in the Stock names into the 2nd dataframe and doing a left or an outer join.

df2['Stock'] = df1['Stock'][:1]
new_df = pd.merge(df2, df1, on  = 'Stock', how = 'outer')
print(new_df)

However, the "Max Dates" from 1st dataframe don't populate in the merged dataframe. Is there an easier way to do this?

Also tried the following-

new_df = pd.concat([df2, df1, axis =1, join = 'outer')

The above is a simple join, but i want rows from df1 repeated for every date entry in df2.

So let's say in the 2nd dataframe, there are n dates, the new dataframe should have these dates and all columns from 1st dataframe repeated n times for each date in 2nd dataframe (for each Stock).

Edit: Please note that the 2nd dataframe has only a list of dates. It doesn't have the list of Stocks, which could potentially be used as common key to do a merge. However, I'm not sure how to bring in Stock name and make it repeat for every date in the 2nd df.

access_nash
  • 35
  • 1
  • 5
  • You can try `pd.merge(df1, df2, how="cross")`. – bb1 Jan 30 '23 at 12:27
  • that doesn't work since to start with i dont have a common key. I need to create a repeat of "Stock" for each date in 2nd df and then i can merge. I'm not sure how to create this repeat. Even the "Merging 101" link doesnt have the answer – access_nash Jan 30 '23 at 13:55
  • Have you tried it? There is no common key with `how="cross"`. – bb1 Jan 30 '23 at 14:23
  • yes i did- it gives an error: "MergeError: No common columns to perform merge on" – access_nash Jan 30 '23 at 14:28
  • It may be an issue with pandas version. `how="cross"` was introduced in 1.2.0. – bb1 Jan 30 '23 at 14:41

0 Answers0