简体   繁体   English

如何在熊猫中将两个具有不同列标签的数据框相乘?

[英]How can I multiply two dataframes with different column labels in pandas?

I'm trying to multiply (add/divide/etc.) two dataframes that have different column labels.我正在尝试将具有不同列标签的两个数据框相乘(加/除/等)。

I'm sure this is possible, but what's the best way to do it?我确信这是可能的,但最好的方法是什么? I've tried using rename to change the columns on one df first, but (1) I'd rather not do that and (2) my real data has a multiindex on the columns (where only one layer of the multiindex is differently labeled), and rename seems tricky for that case...我尝试使用 rename 首先更改一个 df 上的列,但是(1)我宁愿不这样做,并且(2)我的真实数据在列上有一个多索引(其中只有一层多索引的标签不同),对于这种情况,重命名似乎很棘手......

So to try and generalize my question, how can I get df1 * df2 using map to define the columns to multiply together?因此,为了尝试概括我的问题,如何使用map来定义要相乘的列来获得df1 * df2

df1 = pd.DataFrame([1,2,3], index=['1', '2', '3'], columns=['a', 'b', 'c'])
df2 = pd.DataFrame([4,5,6], index=['1', '2', '3'], columns=['d', 'e', 'f'])
map = {'a': 'e', 'b': 'd', 'c': 'f'}

df1 * df2 = ?

I was also troubled by this problem.我也被这个问题困扰。 It seems that the pandas requires matrix multiply needs both dataframes has same column names.似乎熊猫需要矩阵乘法需要两个数据框具有相同的列名。

I searched a lot and found the example in the setting enlargement is add one column to the dataframe.我搜索了很多,发现设置放大中的示例是在数据框中添加一列。

For your question,对于你的问题,

rs = pd.np.multiply(ds2, ds1)

The rs will have the same column names as ds2. rs 将具有与 ds2 相同的列名。

Suppose we want to multiply several columns with other serveral columns in the same dataframe and append these results into the original dataframe.假设我们想将同一数据框中的几列与其他几列相乘,并将这些结果附加到原始数据框中。

For example ds1,ds2 are in the same dataframe ds.例如 ds1,ds2 在同一个数据帧 ds 中。 We can我们可以

ds[['r1', 'r2', 'r3']] = pd.np.multiply(ds[['a', 'b', 'c']], ds[['d', 'e', 'f']])

I hope these will help.我希望这些会有所帮助。

Updated solution now that pd.np is being deprecated: df1.multiply(np.array(df2)现在更新了 pd.np 已被弃用的解决方案: df1.multiply(np.array(df2)

It will keep the column names of df1 and multiply them by the columns of df2 in order它将保留 df1 的列名并按顺序将它们乘以 df2 的列

I just stumbled onto the same problem.我只是偶然发现了同样的问题。 It seems like pandas wants both the column and row index to be aligned to do the element-wise multiplication, so you can just rename with your mapping during the multiplication:似乎熊猫希望列和行索引都对齐以进行元素乘法,因此您可以在乘法期间使用映射rename

>>> df1 = pd.DataFrame([[1,2,3]], index=['1', '2', '3'], columns=['a', 'b', 'c'])
>>> df2 = pd.DataFrame([[4,5,6]], index=['1', '2', '3'], columns=['d', 'e', 'f'])
>>> df1
   a  b  c
1  1  2  3
2  1  2  3
3  1  2  3
>>> df2
   d  e  f
1  4  5  6
2  4  5  6
3  4  5  6
>>> mapping = {'a' : 'e', 'b' : 'd', 'c' : 'f'}
>>> df1.rename(columns=mapping) * df2
   d  e   f
1  8  5  18
2  8  5  18
3  8  5  18

If you want the 'natural' order of columns, you can create a mapping on the fly like:如果您想要列的“自然”顺序,您可以动态创建映射,例如:

>>> df1 * df2.rename(columns=dict(zip(df2.columns, df1.columns)))

for example to do the "Frobenius inner product" of the two matrices, you could do:例如做两个矩阵的“Frobenius 内积”,你可以这样做:

>>> (df1 * df2.rename(columns=dict(zip(df2.columns, df1.columns)))).sum().sum()
96

This is a pretty old question, and as nnsk said, pd.np is being deprecated.这是一个相当古老的问题,正如 nnsk 所说, pd.np已被弃用。

A nice looking solution is df1 * df2.values .一个漂亮的解决方案是df1 * df2.values This will produce the element-wise product of the two dataframes, and keep the column names of df1 .这将产生两个数据帧的元素乘积,并保留df1的列名。

Assuming the index is already aligned, you probably just want to align the columns in both DataFrame in the right order and divide the .values of both DataFrames.假设索引已经对齐,您可能只想以正确的顺序对齐两个 DataFrame 中的列并将两个.values的 .values 相除。

Supposed mapping = {'a' : 'e', 'b' : 'd', 'c' : 'f'} :假设mapping = {'a' : 'e', 'b' : 'd', 'c' : 'f'}

v1 = df1.reindex(columns=['a', 'b', 'c']).values
v2 = df2.reindex(columns=['e', 'd', 'f']).values
rs = DataFrame(v1 / v2, index=v1.index, columns=['a', 'b', 'c'])

另一个假设索引和列定位良好的解决方案:

df_mul= pd.DataFrame(df1.values * df2.values, columns= df1.columns, index= df1.index)

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

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