Malik
Malik

Reputation: 55

How do you repeat each row for a dataframe for each value in a seperate dataframe and then combine the two into a single dataframe?

I have one dataframe with multiple IDs in a single column like this:

enter image description here

And a second dataframe, also with only one column, that has months in it:

enter image description here

How can i combine the two dataframes in python so that each element in the first dataframe with the IDs repeats for each element in the dataframe containing the months?

And i end up with a final dataframe that looks like this:

enter image description here

Upvotes: 0

Views: 627

Answers (1)

yanniskatsaros
yanniskatsaros

Reputation: 468

You can assign a redundant key column to each DataFrame (without mutating the original DataFrames) and join on it, then drop it before returning the final result:

import pandas as pd

df1 = pd.DataFrame({
    'id': list(range(1, 5))
})

df2 = pd.DataFrame({
    'month': ['2010-01', '2010-02', '2010-03']
})

df_merged = pd.merge(
    df1.assign(key=1),
    df2.assign(key=1),
    on='key'
).drop('key', axis=1)
+----+----+---------+
|    | id |  month  |
+----+----+---------+
|  0 |  1 | 2010-01 |
|  1 |  1 | 2010-02 |
|  2 |  1 | 2010-03 |
|  3 |  2 | 2010-01 |
|  4 |  2 | 2010-02 |
|  5 |  2 | 2010-03 |
|  6 |  3 | 2010-01 |
|  7 |  3 | 2010-02 |
|  8 |  3 | 2010-03 |
|  9 |  4 | 2010-01 |
| 10 |  4 | 2010-02 |
| 11 |  4 | 2010-03 |
+----+----+---------+

Upvotes: 2

Related Questions