df = {1,2,3
4,5,6
7,8,9,
10,11,12
}
weights={[1,3,3],[2,2,2],[3,1,1]}
我想将我的 df 与矩阵权重的每一行相乘(所以我将拥有三个不同的 df,每个权重向量一个,并通过保留最大的值行来组合每个 df)。例如:
df0=df * weights[0]={1,6,9
4,15,18,
7,24,27
10,33,36
}
df1=df*wieghts[1]={2,4,6,
8,19,12,
14,16,18,
20,22,24
}
df2=df*wieghts[2]={3,2,3,
12,5,6,
21,8,9,
30,11,12
}
和
final_df_lines=max{df0,df1,df2}={1,6,9 - max line line from df0,
4,15,18, - max line from df0,
7,24,27 - max line from df0,
10,33,36 - max line from df0,
}
在此示例中,所有最大值都来自 df0 ...但它们可以来自三个 df 中的任何一个。 Max line 只是将同一行的数字相加。
我需要将这些事情矢量化(没有任何循环或如果......)我该怎么做?至少有可能吗?我真的需要 welp :( 我在网上搜索了 2 天来做这个……我没有在 python 中工作太久了……
最佳答案
您可以尝试连接
所有权重多列作为一个数据帧,suffix of column
代表每个权重,
并通过根据权重分组
它相乘得到索引的最大总和
使用最大索引权重,您可以将数据帧相乘
df2 = pd.concat([(df*i).add_suffix('__'+str(i)) for i in weights],axis=1).T
0 1 2 3
0__[1, 3, 3] 1 4 7 10
1__[1, 3, 3] 6 15 24 33
2__[1, 3, 3] 9 18 27 36
0__[2, 2, 2] 2 8 14 20
1__[2, 2, 2] 4 10 16 22
2__[2, 2, 2] 6 12 18 24
0__[3, 1, 1] 3 12 21 30
1__[3, 1, 1] 2 5 8 11
2__[3, 1, 1] 3 6 9 12
# by grouping with respect to the weight it multiplied, get max index
a = df2.groupby(df2.index.str.split('__').str[1]).apply(lambda x: x.sum()).idxmax()
# max weights with respect to summation of rows
df['idxmax'] = a.str.slice(1,-1).str.split(',').apply(lambda x: list(map(int,x)))
c [1, 3, 3]
d [1, 3, 3]
3 [1, 3, 3]
4 [1, 3, 3]
dtype: object
df.apply(lambda x: x.loc[df.columns.difference(['idxmax'])] * x['idxmax'],1)
0 1 2
0 1 6 9
1 4 15 18
2 7 24 27
3 10 33 36
关于python - Pandas 将每个数据集行乘以多个向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53131861/