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

Create Table of all Sums from two Arrays in Pandas

I got two arrays:

arr1 = [1,2,3]
arr2 = [5,10]

Now i want to create a Dataframe from the arrays which hold the sum of all combinations:

pd.DataFrame([[6,7,8], [11,12,13]],
                        columns=['1', '2', '3'],
                        index=['5', '10'])
1 2 3
5 6 7 8
10 11 12 13

I know this can be easily done by iterating over the arrays, but I guess there is a built-in function to accomplish the same but way faster.
I’ve already looked in the documentation of different functions like the merge function but without success.

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

>Solution :

We can use numpy broadcasting with addition then build the resulting DataFrame by assigning the index and column names from the lists:

import numpy as np
import pandas as pd

arr1 = [1, 2, 3]
arr2 = [5, 10]

df = pd.DataFrame(
    np.array(arr1) + np.array(arr2)[:, None], index=arr2, columns=arr1
)

Or with add + outer (which works if arr1 and arr2 are lists or arrays):

df = pd.DataFrame(np.add.outer(arr2, arr1), index=arr2, columns=arr1)

*Note if arr1 and arr2 are already arrays (instead of list) it can just look like:

import numpy as np
import pandas as pd

arr1 = np.array([1, 2, 3])
arr2 = np.array([5, 10])

df = pd.DataFrame(arr1 + arr2[:, None], index=arr2, columns=arr1)

All ways produce df:

     1   2   3
5    6   7   8
10  11  12  13
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