Python Pandas:两个数据帧的元素相乘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21022865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:49:21  来源:igfitidea点击:

Pandas: Elementwise multiplication of two dataframes

pythonpandasmultiplicationdataframe

提问by Zhubarb

I know how to do element by element multiplication between two Pandas dataframes. However, things get more complicated when the dimensions of the two dataframes are not compatible. For instance below df * df2is straightforward, but df * df3is a problem:

我知道如何在两个 Pandas 数据帧之间进行逐个元素的乘法。但是,当两个数据帧的维度不兼容时,事情会变得更加复杂。例如下面df * df2很简单,但df * df3有一个问题:

df = pd.DataFrame({'col1' : [1.0] * 5, 
                   'col2' : [2.0] * 5, 
                   'col3' : [3.0] * 5 }, index = range(1,6),)
df2 = pd.DataFrame({'col1' : [10.0] * 5, 
                    'col2' : [100.0] * 5, 
                    'col3' : [1000.0] * 5 }, index = range(1,6),)
df3 = pd.DataFrame({'col1' : [0.1] * 5}, index = range(1,6),)

df.mul(df2, 1) # element by element multiplication no problems

df.mul(df3, 1) # df(row*col) is not equal to df3(row*col)
   col1  col2  col3
1   0.1   NaN   NaN
2   0.1   NaN   NaN
3   0.1   NaN   NaN
4   0.1   NaN   NaN
5   0.1   NaN   NaN

In the above situation, how can I multiply every column of df with df3.col1?

在上述情况下,如何将 df 的每一列与 df3.col1 相乘

My attempt:I tried to replicate df3.col1len(df.columns.values)times to get a dataframe that is of the same dimension as df:

我的尝试:我尝试复制df3.col1len(df.columns.values)时间以获得与以下维度相同的数据帧df

df3 = pd.DataFrame([df3.col1 for n in range(len(df.columns.values)) ])
df3
        1    2    3    4    5
col1  0.1  0.1  0.1  0.1  0.1
col1  0.1  0.1  0.1  0.1  0.1
col1  0.1  0.1  0.1  0.1  0.1

But this creates a dataframe of dimensions 3 * 5, whereas I am after 5*3. I know I can take the transpose with df3.T()to get what I need but I think this is not that the fastest way.

但这会创建一个尺寸为 3 * 5 的数据框,而我在 5 * 3 之后。我知道我可以使用转置df3.T()来获得我需要的东西,但我认为这不是最快的方法。

采纳答案by unutbu

In [161]: pd.DataFrame(df.values*df2.values, columns=df.columns, index=df.index)
Out[161]: 
   col1  col2  col3
1    10   200  3000
2    10   200  3000
3    10   200  3000
4    10   200  3000
5    10   200  3000

回答by Andrey Shokhin

Another way is create list of columns and join them:

另一种方法是创建列列表并加入它们:

cols = [pd.DataFrame(df[col] * df3.col1, columns=[col]) for col in df]
mul = cols[0].join(cols[1:])

回答by The Unfun Cat

A simpler way to do this is just to multiply the dataframe whose colnames you want to keep with the values (i.e. numpy array) of the other, like so:

一种更简单的方法是将要保留其列名的数据帧与另一个的值(即 numpy 数组)相乘,如下所示:

In [63]: df * df2.values
Out[63]: 
   col1  col2  col3
1    10   200  3000
2    10   200  3000
3    10   200  3000
4    10   200  3000
5    10   200  3000

This way you do not have to write all that new dataframe boilerplate.

这样您就不必编写所有新的数据框样板。

回答by Martien Lubberink

This works for me:

这对我有用:

mul = df.mul(df3.c, axis=0)

Or, when you want to subtract (divide) instead:

或者,当您想减去(除)时:

sub = df.sub(df3.c, axis=0)
div = df.div(df3.c, axis=0)

Works also with a nanin df (e.g. if you apply this to the df: df.iloc[0]['col2'] = np.nan)

nan适用于 in df(例如,如果您将其应用于 df:df.iloc[0]['col2'] = np.nan)

回答by Amir Imani

To utilize Pandas broadcasting properties, you can use multiply.

要利用 Pandas 广播属性,您可以使用multiply.

df.multiply(df3['col1'], axis=0)