简体   繁体   中英

Pandas dataframe add 2 dataframes

I need to be able to add the values of two dataframes with the same structure together and form a new dataframe as a result.

eg DF1 + DF2 = DF3

DF1
+------------+----+----+----+
|    date    |  A |  B |  C |
+------------+----+----+----+
| 2017-01-01 | 24 | 15 |  4 |
| 2017-01-02 | 31 | 10 | 12 |
| 2017-01-03 |  9 | 47 |  3 |
+------------+----+----+----+

DF2
+------------+----+----+----+
|    date    |  A |  B |  C |
+------------+----+----+----+
| 2017-01-01 |  4 | 12 | 63 |
| 2017-01-02 | 23 |  0 | 31 |
| 2017-01-03 | 61 | 22 | 90 |
+------------+----+----+----+

DF3
+------------+----+----+----+
|    date    |  A |  B |  C |
+------------+----+----+----+
| 2017-01-01 | 28 | 27 | 67 |
| 2017-01-02 | 64 | 10 | 43 |
| 2017-01-03 | 70 | 69 | 93 |
+------------+----+----+----+

I've been trying to work out how to do this but i'm getting a TypeError

TypeError: unsupported operand type(s) for +: 'datetime.date' and 'datetime.date'

when trying to do:

df3 = df1.add(df2, fill_value=0)

I'm sure i'm missing something simple as it appears to be trying to add the first columns (which is a date and the column I want to match on to add together the values for all other columns) but any help would be greatly appreciated.

You want the date columns to be indices, not normal columns:

df3 = df1.set_index('date').add(df2.set_index('date'), fill_value=0).reset_index()

You use set_index() so that the date columns becomes indices. If you don't want the final dataframe to be date-indexed, you can use reset_index() at the end as @MaxU suggests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM