简体   繁体   English

将 pandas dataframe 的每一行乘以另一行 dataframe

[英]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.所以我想将 dataframe 的每一行与乘数向量相乘,我正在管理,但它看起来很难看。 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: “它看起来很丑”是主观的,也就是说,如果你想将 dataframe 的所有行与你需要的其他东西相乘:

  • 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 )兼容形状的 dataframe (和兼容索引,因为它们在 pandas 中的操作之前对齐,这就是为什么df_a*df_b.T仅适用于公共索引: 0

  • a 1D vector, which in pandas is a Series一维向量,在 pandas 中是一个系列

Using a Series:使用系列:

df_a*df_b[0]

output: 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:当然,如果您真的不需要 2D 容器,最好直接定义 Series:

s = pd.Series([2,2,1])
df_a*s

Just for the beauty, you can use Einstein summation :只是为了美观,您可以使用Einstein summation

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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