Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merge Datasets by Group in Python

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading