0

I have two Dataframes:

df1:
            A    B
Date
12/2020     1    0
13/2020     1    1


df2:
            A    B    C
Date
12/2020     0    0    1
13/2020     1    0    1

I want to add the values of df1 into df2 to get the following:

df3:
            A    B    C
Date
12/2020     1    0    1
13/2020     2    1    1

How can can I do this using something like pd.join or pd.merge? (I have two large dataframes)

Thank you.

MathMan 99
  • 665
  • 1
  • 7
  • 19
  • Did you look here ?: https://stackoverflow.com/questions/11106823/adding-two-pandas-dataframes – Aayush Upadhyay Jul 28 '21 at 19:00
  • Does this answer your question? [Adding two pandas dataframes](https://stackoverflow.com/questions/11106823/adding-two-pandas-dataframes) – ThePyGuy Jul 28 '21 at 19:03
  • 1
    if `add` does not work on your actual dataset but does work on this example, please update your question to have an example where add does not work. – Henry Ecker Jul 28 '21 at 19:14

3 Answers3

0

Just use add():

>>> df1.add(df2, fill_value=0)
not_speshal
  • 22,093
  • 2
  • 15
  • 30
  • That doesn't work. df2['C'] returns "nan" rather than "1". Also, the indices for df1 and df2 are not of the same length. I think something like .merge or .concat should work, but I'm not so sure. – MathMan 99 Jul 28 '21 at 19:10
  • @MathMan99 - You should provide the appropriate examples in your question. This should work regardless of of the your DataFrame sizes. – not_speshal Jul 28 '21 at 19:12
0

in pandas there is a function

x.add(y, fill_value=0)

import pandas as pd

df1:
            A    B
Date
12/2020     1    0
13/2020     1    1


df2:
            A    B    C
Date
12/2020     0    0    1
13/2020     1    0    1

df3 = df1.add(df2, fill_value=0)
Out: 
df3:
            A    B    C
Date
12/2020     1    0    1
13/2020     2    1    1
```

Try this 
femiir
  • 190
  • 1
  • 2
  • 15
  • That doesn't work. df2['C'] returns "nan" rather than "1". Also, the indices for df1 and df2 are not of the same length. I think something like .merge or .concat should work, but I'm not so sure. – MathMan 99 Jul 28 '21 at 19:07
  • you should consider merge then – femiir Jul 28 '21 at 19:41
0

Try:

import pandas as pd
import pandasql as psql

df1 = 'Table1' #Insert dataframe
df2 = 'Table2' #Insert dataframe

#Create two dataframes to be joined on final dataframe
t1 = psql.sqldf('''
SELECT
   CONCAT(Date, A, B) AS Full1
   Date
   , A
   , B
FROM df1
''')

t2 = psql.sqldf('''
SELECT
   CONCAT(Date, A, B) Full2
   , Date
   , A
   , B
   , C
FROM df2
''')

#Create Full Dataframe
full_df = psql.df('''
SELECT
  tt.Full1
  , tt.Date
  , tt.A
  , tt.B
  , tt2.C
FROM t1 tt
LEFT JOIN t2 tt2 ON tt.Full1 = tt2.Full2
''')

#Create Final Dataframe
Final = psql.df('''
SELECT
   Date
   , A
   , B
   , C
FROM full_df
''')

#Save final result to csv
Final.to_csv(Index=False, 'file_name.csv')