sum up two pandas dataframes with different indexes element by element

2024/10/3 14:20:19

I have two pandas dataframes, say df1 and df2, of some size each but with different indexes and I would like to sum up the two dataframes element by element. I provide you an easy example to better understand the problem:

dic1 = {'a': [3, 1, 5, 2], 'b': [3, 1, 6, 3], 'c': [6, 7, 3, 0]}
dic2 = {'c': [7, 3, 5, 9], 'd': [9, 0, 2, 5], 'e': [4, 8, 3, 7]}
df1 = pd.DataFrame(dic1)
df2 = pd.DataFrame(dic2, index = [4, 5, 6, 7])

so df1 will be

   a  b  c
0  3  3  6
1  1  1  7
2  5  6  3
3  2  3  0

and df2 will be

   c  d  e
4  7  9  4
5  3  0  8
6  5  2  3
7  9  5  7

now if type

df1 + df2

what I get is

    a   b   c   d   e0 NaN NaN NaN NaN NaN1 NaN NaN NaN NaN NaN2 NaN NaN NaN NaN NaN3 NaN NaN NaN NaN NaN4 NaN NaN NaN NaN NaN5 NaN NaN NaN NaN NaN6 NaN NaN NaN NaN NaN7 NaN NaN NaN NaN NaN

How can I make pandas understand that I want to sum up the two dataframe just element by element?

Answer

UPDATE: much better solution from piRSquared:

In [39]: df1 + df2.values
Out[39]:a   b   c
0  10  12  10
1   4   1  15
2  10   8   6
3  11   8   7

Old answer:

In [37]: df1.values + df2.values
Out[37]:
array([[10, 12, 10],[ 4,  1, 15],[10,  8,  6],[11,  8,  7]], dtype=int64)In [38]: pd.DataFrame(df1.values + df2.values, columns=df1.columns)
Out[38]:a   b   c
0  10  12  10
1   4   1  15
2  10   8   6
3  11   8   7
https://en.xdnf.cn/q/70713.html

Related Q&A

Urwid: make cursor invisible

Im using urwid, which is a Python "framework" for designing terminal user interfaces in ncurses. Theres one thing though that Im not able to do in urwid that was easy in curses - make the cur…

How do I use scipy.weave.inline together with external C libraries?

I am trying to understand weave.inline to wrap C code in my Python programs. The code below simply takes the Numpy array and multiplicates all of its elements by 2.inl.py import numpy import scipy.weav…

sqlalchemy multiple foreign keys to same table

I have a postgres database that looks something like this:Table "public.entities"Column | Type | Modifiers ---------------+---…

Django - Return a file from Root folder via a URL

I purchased a SSL cert online and now ind the mid of verifying my host. How it works is:It gives me a file I have to make that file accessible through a specific URL on my host. If the content of the f…

Flask deployement on lighttpd and raspberry pi

Im trying to deploy a hello flask app to a raspberry pi using lighttpd fastCGI.I followed the instructions on the http://flask.pocoo.org/docs/0.10/deploying/fastcgi/ to the best of my abilityHere is my…

Django admin asks for login after every click

Im working on a Django app hosted on Heroku. Im able to login to the admin with my username, password. But on every single click (or on each click after a few seconds) it redirects me to the login page…

Change numerical Data to Categorical Data - Pandas [duplicate]

This question already has answers here:How to create new values in a pandas dataframe column based on values from another column(2 answers)Closed 6 years ago.I have a pandas dataframe which has a numer…

Why is dataclasses.astuple returning a deepcopy of class attributes?

In the code below the astuple function is carrying out a deep copy of a class attribute of the dataclass. Why is it not producing the same result as the function my_tuple? import copy import dataclass…

customize dateutil.parser century inference logic

I am working on old text files with 2-digit years where the default century logic in dateutil.parser doesnt seem to work well. For example, the attack on Pearl Harbor was not on dparser.parse("12…

How can I check a Python unicode string to see that it *actually* is proper Unicode?

So I have this page:http://hub.iis.sinica.edu.tw/cytoHubba/Apparently its all kinds of messed up, as it gets decoded properly but when I try to save it in postgres I get:DatabaseError: invalid byte seq…