使用向量获取数据帧的点积,并在Pandas中返回数据帧 [英] Get dot-product of dataframe with vector, and return dataframe, in Pandas

查看:186
本文介绍了使用向量获取数据帧的点积,并在Pandas中返回数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在方法 dot() 在官方文档。然而,方法是在那里,我可以使用它。为什么是这样?

I am unable to find the entry on the method dot() in the official documentation. However the method is there and I can use it. Why is this?

在这个主题上,有没有办法计算数据框中每一行的元素乘法与另一个向量? (并获得一个数据框返回?),即类似于 dot(),而不是计算点积,一

On this topic, is there a way compute an element-wise multiplication of every row in a data frame with another vector? (and obtain a dataframe back?), i.e. similar to dot() but rather than computing the dot product, one computes the element-wise product.

推荐答案

下面是一个如何将DataFrame乘以一个向量的示例:

Here is an example of how to multiply a DataFrame by a vector:

In [60]: df = pd.DataFrame({'A': [1., 1., 1., 2., 2., 2.], 'B': np.arange(1., 7.)})

In [61]: vector = np.array([2,2,2,3,3,3])

In [62]: df.mul(vector, axis=0)
Out[62]: 
   A   B
0  2   2
1  2   4
2  2   6
3  6  12
4  6  15
5  6  18

这篇关于使用向量获取数据帧的点积,并在Pandas中返回数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆