Python 在其他列上匹配pandas.DataFrame.mul

Python 在其他列上匹配pandas.DataFrame.mul,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据帧(忽略数据帧的索引) 还有熊猫系列 Ind 1 1.208333 2 0.857143 dtype: float64 我想将DataFrame的Val列的每个元素与具有相同Ind值的序列元素相乘。我将如何处理这个问题?pandas.DataFrame.mul仅在索引上匹配,但我不想转换数据帧。看起来像pandas.DataFrame.join可以解决您的问题: temp = df.join(the_series,on='Ind', lsuffix='_orig') d

我有一个数据帧(忽略数据帧的索引)

还有熊猫系列

Ind
1    1.208333
2    0.857143
dtype: float64

我想将DataFrame的Val列的每个元素与具有相同Ind值的序列元素相乘。我将如何处理这个问题?pandas.DataFrame.mul仅在索引上匹配,但我不想转换数据帧。

看起来像
pandas.DataFrame.join
可以解决您的问题:

temp = df.join(the_series,on='Ind', lsuffix='_orig')
df['ans'] = temp.Val*temp.Ind
输出

    Tab  Ind  Com  Val        ans
4   BAS    1    1   10  12.083330
5   BAS    1    2    5   6.041665
6   BAS    2    1   20  17.142860
8   AIR    1    1    5   6.041665
9   AIR    1    2    2   2.416666
11  WTR    1    1    2   2.416666
12  WTR    2    1    1   0.857143
或者使用更紧凑的语法()


看起来像
pandas.DataFrame.join
可以解决您的问题:

temp = df.join(the_series,on='Ind', lsuffix='_orig')
df['ans'] = temp.Val*temp.Ind
输出

    Tab  Ind  Com  Val        ans
4   BAS    1    1   10  12.083330
5   BAS    1    2    5   6.041665
6   BAS    2    1   20  17.142860
8   AIR    1    1    5   6.041665
9   AIR    1    2    2   2.416666
11  WTR    1    1    2   2.416666
12  WTR    2    1    1   0.857143
或者使用更紧凑的语法()

缩短行:-)
df1['New']=df1.Ind.map.values*df1.Val
缩短行:-)
df1['New']=df1.Ind.map.values*df1.Val