Get a DataFrame with calculated value from 2 lists

Question:

I have the following 2 lists:

A=list(range(2018, 2023))

B=list(range(1,13))

Corresponding to A -> (2018,2019,2020,2021,2022) and 
                 B -> (1,2,3,4,5,6,7,8,9,10,11,12).

I need to get the following DataFrame:

ColumnA
201801
201802
201803
201804
201805
201806
201807
201808
201809
201810
201811
201812
201901
201902

And so on.

I can calculate those values like (A*100)+B, but I dont know how to obtain all values at once.

Asked By: Aragorn64

||

Answers:

cross join ?:

df1 = pd.DataFrame(A, columns=['Year'])
df2= pd.DataFrame(B, columns=['Month'])

res = df1.merge(df2, how='cross')

# and if you want to concat them :
res['Column1'] = res['Year'].astype(str)+ res['Month'].astype(str).str.zfill(2)

output:

> 
    Year  Month Column1
0   2018      1  201801
1   2018      2  201802
2   2018      3  201803
3   2018      4  201804
4   2018      5  201805
5   2018      6  201806
6   2018      7  201807
7   2018      8  201808
8   2018      9  201809
9   2018     10  201810
10  2018     11  201811
...
Answered By: eshirvana
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.