Skip to content
Advertisement

Join to dataframes based on index where the second dataframe has repeated indexes related to the first dataframe

I have two data frames where first dataframe has index starting from zero. The second dataframe has repeated indexes starting from zero. I want to to join the two dataframes based on their indexes. First dataframe is like this

JavaScript

The second dataframe is

JavaScript

I want to join these two dataframes based on index i.e the new dataframe should look like

JavaScript

What this essentially is I need to replicate the rows of dataframe 1 based on how many times the same index is repeated in the second dataframe. As we can see, in second dataframe, zero index appears twice, so we replicate the rows of zero index of dataframe 1 twice and then join the dataframes and so on. In the end we can reset the index(that I know about).

I am attaching the links of both the dataframes for the reference. Link for first dataframe https://drive.google.com/file/d/1DqxhnMM8R21Olm9zeRJeDgua_ozoRp8P/view?usp=sharing

Link for second dataframe https://drive.google.com/file/d/1sX5xcTeovVqXtZgSZ5cTC5JRdUvaw7gd/view?usp=sharing

I cant figure out how to proceed with such tasks. Please help me out.

Advertisement

Answer

Pandas join would do just what you request.

Consider an example

JavaScript

We have two dataframes:

JavaScript

and

JavaScript

Now

JavaScript

Would be the dataframe

JavaScript
Advertisement