Search code examples
pandascross-join

How to cross join 2 series to make it a lookup table in pandas?


I am trying to create a reference table containing a cross join of id's and dates and will use this table as left join table to perform all the operations. Please could you advise how to make dataframe from 2 series by doing cross join of series.

i have created series 1 as :

df1 = pd.Series(pd.date_range(start='1/1/2017', end='1/1/2019', freq='MS'))
df2 = pd.Series(["AN", "BN", "CN"])

I want a df like:

id dt
AN 1-1-2017
AN 1-2-2017
..
BN 1-1-2017
BN 1-2-2017
..
CN 1-1-2017
CN 1-2-2017
and so on..

Solution

  • Use itertools.product with DataFrame constructor:

    from  itertools import product
    df = pd.DataFrame(list(product(df2, df1)), columns=['id','dt'])
    #pandas 0.25+ list should be omit
    #df = pd.DataFrame((product(df2, df1)), columns=['id','dt'])
    print (df)
        id         dt
    0   AN 2017-01-01
    1   AN 2017-02-01
    2   AN 2017-03-01
    3   AN 2017-04-01
    4   AN 2017-05-01
    ..  ..        ...
    70  CN 2018-09-01
    71  CN 2018-10-01
    72  CN 2018-11-01
    73  CN 2018-12-01
    74  CN 2019-01-01
    
    [75 rows x 2 columns]