Home > Mobile >  Merge Datasets by Group in Python
Merge Datasets by Group in Python

Time:06-30

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

CodePudding user response:

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)
  • Related