0

I have the two datasets below and would like to merge them by group in df2. I am not sure how to merge them such that the dates reset for each group (ie. start from the 1st date and end at the last date for each group).

df1:

Date
2021-01-01
2021-01-02
2021-01-03

df2:

Group
A
B
C

dfdesired:

Date Group
2021-01-01 A
2021-01-02 A
2021-01-03 A
2021-01-01 B
2021-01-02 B
2021-01-03 B
2021-01-01 C
2021-01-02 C
2021-01-03 C

any help would be appreciated

  • 1
    Does this answer your question? https://stackoverflow.com/questions/53699012/performant-cartesian-product-cross-join-with-pandas – dsillman2000 Jun 29 '22 at 21:21
  • It did work but quick question about it Are those two different steps in the accepted answer or two different ways to do it entirely. I am talking about the "pandas <= 1.1.X`" and "pandas <= 1.2.X" I ran them both and they gave me the same output. –  Jun 29 '22 at 21:31
  • 1
    If both versions of the code worked, then you're on the newer pandas 1.2.X distribution, so either will work. – dsillman2000 Jun 29 '22 at 21:40

1 Answers1

2

You need to perform cross join. Here is what I would suggest:

# Creating a dummy column to perform a join
df1["Dummy"] = 1
df2["Dummy"] = 1

# to obtain the cross join we will merge on the dummy column and drop it.
df_desired= pd.merge(df1, df2, on ='Dummy').drop("Dummy", 1)
Deepansh Arora
  • 724
  • 1
  • 3
  • 15