'Join tables and create combinations in python

In advance: Sorry, the title is a bit fuzzy

PYTHON

I have two tables. In one there are unique names for example 'A', 'B', 'C' and in the other table there is a Time series with months example 10/2021, 11/2021, 12/2021. I want to join the tables now that I have all TimeStemps for each name. So the final data should look like this:

Month Name
10/2021 A
11/2021 A
12/2021 A
10/2021 B
11/2021 B
12/2021 B
10/2021 C
11/2021 C
12/2021 C


Solution 1:[1]

If you are only trying to get the cartesian product of the values - you can do it using itertools.product

import pandas as pd
from itertools import product
df1 = pd.DataFrame(list('abcd'), columns=['letters'])
df2 = pd.DataFrame(list('1234'), columns=['numbers'])

df_combined = pd.DataFrame(product(df1['letters'], df2['numbers']), columns=['letters', 'numbers'])

output

   letters numbers
0        a       1
1        a       2
2        a       3
3        a       4
4        b       1
5        b       2
6        b       3
7        b       4
8        c       1
9        c       2
10       c       3
11       c       4
12       d       1
13       d       2
14       d       3
15       d       4

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mortz