pandas 如何在熊猫中将两个数据框与不同的列标签相乘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12521909/
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
How can I multiply two dataframes with different column labels in pandas?
提问by jmloser
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...
我确信这是可能的,但最好的方法是什么?我已经尝试使用重命名来首先更改一个 df 上的列,但是 (1) 我宁愿不这样做,并且 (2) 我的真实数据在列上有一个多索引(其中只有一个多索引层的标签不同),并且在这种情况下重命名似乎很棘手......
So to try and generalize my question, how can I get df1 * df2using mapto define the columns to multiply together?
所以,试图概括我的问题,我怎样才能df1 * df2使用map定义的列乘在一起吗?
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 = ?
采纳答案by Chang She
Assuming the index is already aligned, you probably just want to align the columns in both DataFrame in the right order and divide the .valuesof both DataFrames.
假设索引已经对齐,您可能只想以正确的顺序对齐两个 DataFrame 中的列并将两个 DataFrame 的列.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'])
回答by BearPy
I was also troubled by this problem. It seems that the pandas requires matrix multiply needs both dataframes has same column names.
我也被这个问题困扰。似乎Pandas需要矩阵乘法需要两个数据帧具有相同的列名。
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. We can
例如 ds1,ds2 在同一个数据帧 ds 中。我们可以
ds[['r1', 'r2', 'r3']] = pd.np.multiply(ds[['a', 'b', 'c']], ds[['d', 'e', 'f']])
I hope these will help.
我希望这些会有所帮助。
回答by patricksurry
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 renamewith your mapping during the multiplication:
我只是偶然发现了同样的问题。似乎Pandas希望列和行索引都对齐以进行元素乘法,因此您可以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

