Reputation: 55
I have one dataframe with multiple IDs in a single column like this:
And a second dataframe, also with only one column, that has months in it:
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:
Upvotes: 0
Views: 627
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