2013-03-15 115 views
6

我想要做一個熊貓數據幀的矩陣multiplcation等一系列矩陣乘法和系列

df = pandas.DataFrame({'a':[4,1,3], 'b':[5,2,4]},index=[1,2,3]) 
ser = pandas.Series([0.6,0.4]) 

df是,

a b 
1 4 5 
2 1 2 
3 3 4 

SER是,

0 0.6 
1 0.4 

我期望的結果是矩陣產品,就像這樣

答是,

我可以用numpy的點運算符和重建我的數據幀

c = a.values.dot(b.transpose()) 
c = pandas.DataFrame(c, index = a.index, columns = ['ans']) 
print c 


    ans 
1 4.4 
2 1.4 
3 3.4 

是否有大熊貓本地方法來做到這一點做到這一點?

回答

11

大熊貓含蓄一系列的指數對齊,使用點功能

In [3]: df = pd.DataFrame({'a' : [4,1,3], 'b' : [5,2,4]},index=[1,2,3]) 

In [4]: s = pd.Series([0.6,0.4],index=['a','b']) 

In [5]: df.dot(s) 
Out[5]: 
1 4.4 
2 1.4 
3 3.4