Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get a DataFrame with calculated value from 2 lists

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading