pandas在数据帧和系列之间相乘(列)

时间:2016-03-21 07:39:29

标签: python numpy pandas

例如,我有一个像

这样的数据框
[ 2 2 2
  3 3 3 
  4 4 4
  5 5 5] 

我有一个系列[2 1 2 1] 我怎样才能得到

[ 4 4 4
  3 3 3
  8 8 8
  5 5 5]

2 个答案:

答案 0 :(得分:2)

您可以尝试mul,如果您需要将DataFrame转换为numpy array,请使用values

print df
   0  1  2
0  2  2  2
1  3  3  3
2  4  4  4
3  5  5  5

s = pd.Series([2, 1, 2, 1])
print s
0    2
1    1
2    2
3    1
dtype: int64

print df.mul(s, axis=0)
   0  1  2
0  4  4  4
1  3  3  3
2  8  8  8
3  5  5  5

print df.mul(s, axis=0).values
[[4 4 4]
 [3 3 3]
 [8 8 8]
 [5 5 5]]

答案 1 :(得分:1)

你可以使用numpy broadcasting

来做到这一点
auto hilightReelDlg = new HighlightReelDialog( this->highlightManager,
                                               this->soloshotAnalyzer->getNoVideoArea(),
                                               this->videoFilePaths,
                                               this->SortingOptions,
                                               this );
ui->btnShare->setEnabled(false);
if (hilightReelDlg != NULL )
{
    int NumberHilights = this->mainAppLinker->getHighlightJumper()->getActiveHighlightCount();

    ui->hilightProgressBar->setMaximum(NumberHilights);

    QObject::connect(hilightReelDlg, &HighlightReelDialog::send_Init_Progress, this, [=](int inc) 
    { 
        on_Hilight_Init_Progress(inc); 
    });
    QObject::connect(hilightReelDlg, &HighlightReelDialog::initialization_finished, this, [=]() { on_Hilight_Init_Finished(); });

    hilightReelDlg->ProcessVideoPlayers();
    //QDesktopWidget Class
    QDesktopWidget desk;
    QRect screenres = desk.screenGeometry(0);
    hilightReelDlg->setGeometry(QRect(0.10*screenres.width(), 67, 0.79*screenres.width(), 0.86*screenres.height()));
    hilightReelDlg->setModal(false);
    hilightReelDlg->show();

}
ui->btnShare->setEnabled(true);

<强>定时:

In [36]: df.values
Out[36]:
array([[2, 2, 2],
       [3, 3, 3],
       [4, 4, 4],
       [5, 5, 5]], dtype=int64)

In [37]: s.values
Out[37]: array([2, 1, 2, 1], dtype=int64)

In [38]: df.values * s.values[:,None]
Out[38]:
array([[4, 4, 4],
       [3, 3, 3],
       [8, 8, 8],
       [5, 5, 5]], dtype=int64)