简体   繁体   中英

Multiplying each row of a pandas dataframe by another row dataframe

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

"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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM