Search code examples
pandaspandas-groupbysklearn-pandas

Merging 2 pandas tables and using them


I have 2 panda tables, table A which is something like that:

Date  a    b   c    d   e
0
...
.
.
.
2n

and table B which has something like that

Date f  g  k  h   i   j
2
.
.
..
.
.
2n-3
.

the issue is that each table has totally different dates, and totally different columns, but I want to get a new table with 0 if it is a column which it didnt had before, and also a new binary column(lets cal it MARK) which marks if its from table A or B. something like this:

Date   MARK  a    b   c    d   e  f  g  e  h   i   j
0        1   .    .    .   .   .  0  0  0  0   0    0
.
2        0   0    0   0    0   0  .  .   .  .  .    .
.
.
.
.
.
2n-3        0   0    0   0    0   0  .  .   .  .  .    .
.
.
2n        1   .    .    .   .   .  0  0  0  0   0    0 

And i need it to be sorted by the dates... please help me with this... Thanks


Solution

  • Add the 'Mark' column before :

    df1['Mark'] = 0
    df2['Mark'] = 1
    

    Then pd.concat does exactly what you want to do :

    df = pd.concat([df1, df2], axis = 0, sort= True)
    

    Note : Make sure your dates are the index for both :

    df1 = df1.set_index('date_column')
    df2 = df2.set_index('date_column')