0

So I want to multiply each row of a dataframe with a multiplier vector, and I am managing, but it looks ugly. Can this be improved?

import pandas as pd
import numpy as np


# original data
df_a = pd.DataFrame([[1,2,3],[4,5,6]])
print(df_a, '\n')

# multiplier vector
df_b = pd.DataFrame([2,2,1])
print(df_b, '\n')

# multiply by a list - it works
df_c = df_a*[2,2,1]
print(df_c, '\n')

# multiply by the dataframe - it works
df_c = df_a*df_b.T.to_numpy()
print(df_c, '\n')
1
  • Since your dataframes have not the same shape, I don't know how you could do that differently. Commented Jan 15, 2022 at 20:24

2 Answers 2

3

"It looks ugly" is subjective, that said, if you want to multiply all rows of a dataframe with something else you either need:

  • a dataframe of a compatible shape (and compatible indices, as those are aligned before operations in pandas, which is why df_a*df_b.T would only work for the common index: 0)

  • a 1D vector, which in pandas is a Series

Using a Series:

df_a*df_b[0]

output:

   0   1  2
0  2   4  3
1  8  10  6

Of course, better define a Series directly if you don't really need a 2D container:

s = pd.Series([2,2,1])
df_a*s
Sign up to request clarification or add additional context in comments.

Comments

2

Just for the beauty, you can use Einstein summation:

>>> np.einsum('ij,ji->ij', df_a, df_b)

array([[ 2,  4,  3],
       [ 8, 10,  6]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.