Home > Blockchain >  Multiplying each row of a pandas dataframe by another row dataframe
Multiplying each row of a pandas dataframe by another row dataframe

Time:01-16

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')

CodePudding user response:

"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

CodePudding user response:

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]])
  •  Tags:  
  • Related