2017-07-31 76 views
4

我有我要乘2大熊貓據幀:如何在熊貓中將1 * m DataFrame乘以n * m DataFrame?

frame_score: 
    Score1 Score2 
0  100  80 
1 -150  20 
2 -110  70 
3  180  99 
4  125  20 

frame_weights: 
    Score1 Score2 
0  0.6  0.4 

我想:

import pandas as pd 
import numpy as np 

frame_score = pd.DataFrame({'Score1' : [100, -150, -110, 180, 125], 
         'Score2' : [80, 20, 70, 99, 20]}) 

frame_weights = pd.DataFrame({'Score1': [0.6], 'Score2' : [0.4]}) 

print('frame_score: \n{0}'.format(frame_score)) 
print('\nframe_weights: \n{0}'.format(frame_weights)) 

# Each of the following alternatives yields the same results 
frame_score_weighted = frame_score.mul(frame_weights, axis=0) 
frame_score_weighted = frame_score * frame_weights 
frame_score_weighted = frame_score.multiply(frame_weights, axis=1) 

print('\nframe_score_weighted: \n{0}'.format(frame_score_weighted)) 

回報:

frame_score_weighted: 
    Score1 Score2 
0 60.0 32.0 
1  NaN  NaN 
2  NaN  NaN 
3  NaN  NaN 
4  NaN  NaN 

的1到4行是NaN。我怎樣才能避免這種情況?例如,第1行應該是-90 8(-90 = -150 * 0.6; 8 = 20 * 0.4)。

例如,Numpy可能廣播以匹配尺寸。

回答

3

編輯:任意尺寸,嘗試使用values來操縱類似陣列的方式dataframes值:

# element-wise multiplication 
frame_score_weighted = frame_score.values*frame_weights.values 

# change to pandas dataframe and rename columns 
frame_score_weighted = pd.DataFrame(data=frame_score_weighted, columns=['Score1','Score2']) 

#Out: 
    Score1 Score2 
0 60.0 32.0 
1 -90.0  8.0 
2 -66.0 28.0 
3 108.0 39.6 
4 75.0  8.0 

只需使用一些額外的索引,以確保您提取所需權重標當你做乘法。

frame_score['Score1'] = frame_score['Score1']*frame_weights['Score1'][0] 
frame_score['Score2'] = frame_score['Score2']*frame_weights['Score2'][0] 

frame_score 
#Out: 
    Score1 Score2 
0 60.0 32.0 
1 -90.0  8.0 
2 -66.0 28.0 
3 108.0 39.6 
4 75.0  8.0 
2

默認情況下,當pd.DataFramepd.Series相乘,pandas對準pd.Seriespd.DataFrame的列的索引。因此,我們通過訪問第一行從frame_weights獲得相關的pd.Series

frame_score * frame_weights.loc[0] 

    Score1 Score2 
0 60.0 32.0 
1 -90.0  8.0 
2 -66.0 28.0 
3 108.0 39.6 
4 75.0  8.0 

您可以在適當的位置

frame_score *= frame_weights.loc[0] 
編輯 frame_score