Dot产品过滤了数据帧pandas

时间:2018-04-18 10:54:12

标签: python pandas numpy filter

我们假设我有两个像这样的pandas数据帧:

df1 = pd.DataFrame({'Component': ['A','B','C','D'],'Theme': ['T1','T2','T3','T3'],'Weights': [0.5,0.1,0.1,0.3]},index=[0, 1, 2, 3])

df2 = pd.DataFrame({'A': [-0.1,0.05,-0.07,-0.5,0.02],'B': [-0.3,0.02,-0.01,-0.4,0.01],'C': [-0.8,0.00,-0.01,-0.1,0.07],'D': [-0.08,0.1,-0.01,-0.05,0.03],},index=[0, 1, 2, 3,4])

我正在寻找一种“智能方法”来计算按主题分组的两个数据帧的“sumproduct”(或点积)。问题是第二个数据帧中缺少“主题”,链接两个数据帧的唯一方法是按组件。

我们举个例子: 对于主题T3,结果应该是这样的数据框:

df3=pd.DataFrame({'T3':[-0.104,0.03,-0.004,-0.025,0.016]},index=[0, 1, 2, 3,4])

计算详情如下(df1的产品(按主题T3过滤)和df2:

0.1*-0.8+0.3*-0.08 = -0.104

0.1*0+0.3*0.1=0.03

0.1*-0.01+0.3*-0.01=-0.004

0.1*-0.1+0.3*-0.05=-0.025

0.1*0.07+0.3*0.03=0.016

如果我们在两个numpy数组中转换df1和df2,我们可以做转置(df1)和转置(df2)的点积。那会很完美。但是,由于我需要按主题进行过滤,因此我陷入困境。

总体而言,我想拥有3个数据帧(因为我有3个主题)

有什么建议吗?谢谢。

1 个答案:

答案 0 :(得分:1)

在两侧设置相同的索引,需要dot产品:

df1 = df1.set_index('Component')

使用where应用所需的过滤条件,然后用0:

填充NA值
df2.dot(df1.where(df1.Theme=='T3').Weights.fillna(0))

0   -0.104
1    0.030
2   -0.004
3   -0.025
4    0.016
dtype: float64