Home > Blockchain >  Create Table of all Sums from two Arrays in Pandas
Create Table of all Sums from two Arrays in Pandas

Time:11-15

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.

CodePudding user response:

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
  • Related