簡體   English   中英

如何在pandas中將n * m DataFrame與1 * m DataFrame相乘?

[英]How can I multiply a n*m DataFrame with a 1*m DataFrame in pandas?

我有2個pandas 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可以廣播以匹配尺寸。

編輯:對於任意維度,嘗試使用values以類似數組的方式處理數據幀的值:

# 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

默認情況下,當pd.DataFrame乘以pd.Seriespandas pd.Series的索引與pd.Series的列pd.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_score *= frame_weights.loc[0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM