4

I'm new to numpy and I'm currently working on a modeling project for which I have to perform some calculations based on two different data sources. However until now I haven't managed to figure out how I could multiply all the individual values to each other:

I have two data frames

One 2D-dataframe:

df1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

One 1D-dataframe:

df2 = np.array([1, 2, 3, 4, 5])

I would like to multiply all the individual values within the first dataframe (df1) separately with all the values that are stored within the second dataframe in order to create a data cube / new 3D-dataframe that has the shape 5x3x3:

df3 = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[2, 4, 6], [8, 10, 12], [14, 16, 18]], ..... ])

I tried different methods but every time I failed to obtain something that looks like df3.

x = np.array([[1, 2, 3],
     [4, 5, 6],
    [7, 8, 9]])
y = np.array([1, 2, 3, 4, 5])



z = y


for i in range(len(z)):
    z.iloc[i] = x

for i in range(0, 5):
    for j in range(0, 3):
        for k in range(0, 3):
            z.iloc[i, j, k] = y.iloc[i] * x.iloc[j, k]

print(z)

Could someone help me out with some example code? Thank you!

  • 3
    Those aren't datafamres: they're lists – Mad Physicist Dec 26 '21 at 22:50
  • 2
    Don't bother saying "I tried different methods" if you don't show evidence. Please see [mcve] – Mad Physicist Dec 26 '21 at 22:51
  • looks like a for loop problem – Mabadai Dec 26 '21 at 22:52
  • 1
    @Mabadai. That's probably not the best way to solve it – Mad Physicist Dec 26 '21 at 22:56
  • 2
    Please don't make up terms or use irrelevant tags. Numpy doesn't have dataframes and your question doesn't have jumped numpy – Mad Physicist Dec 26 '21 at 22:57
  • @MadPhysicist Agreed. Could you please elaborate ! This will help a lot :) – Mabadai Dec 26 '21 at 23:00
  • 1
    Can't even understand why this question is getting multiple upvotes :) – j1-lee Dec 26 '21 at 23:02
  • @j1-lee mm... outperform an everyday task. Could you post here your most efficient solution ? :) – Mabadai Dec 26 '21 at 23:05
  • 2
    I am afraid I don't understand. I am not talking about the solutions here; they are nice. I am talking about the question. Now that the OP attached their effort, so I agree with upvotes afterwards. But before that I was just curious about why the question was getting upvotes even in the absence of effort and clear terminology. – j1-lee Dec 26 '21 at 23:11
  • 1
    @j1-lee you're not alone. I think it must be mostly random. it's even weirder [here](https://stackoverflow.com/q/69936168/17242583) –  Dec 26 '21 at 23:13

1 Answers1

5

Try this:

df1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
df2 = np.array([1, 2, 3, 4, 5])

df3 = df1 * df2[:, None, None]

Output:

>>> df3
array([[[ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9]],

       [[ 2,  4,  6],
        [ 8, 10, 12],
        [14, 16, 18]],

       [[ 3,  6,  9],
        [12, 15, 18],
        [21, 24, 27]],

       [[ 4,  8, 12],
        [16, 20, 24],
        [28, 32, 36]],

       [[ 5, 10, 15],
        [20, 25, 30],
        [35, 40, 45]]])