Pandas将数据框中的多个多索引列乘以另一列

时间:2015-02-05 11:47:59

标签: python pandas

给出以下数据框

## example of how to get a "specific" axis point using xs
import pandas as pd
date = pd.bdate_range('2013-01-14','2013-01-20').repeat(5)
sector = [1]*3
sector.extend([2]*2)
sector = sector * 5
instrument = list('ABCDE')*5
port = ['pf']*25
data = xrange(25)
df = pd.DataFrame(dict(port=port,sector=sector,instrument=instrument,date=date,data=data))
port = ['bm']*25
df1 = pd.DataFrame(dict(port=port,sector=sector,instrument=instrument,date=date,data=data))
df = pd.concat([df,df1],axis=0)
df = df.set_index(['port','sector','instrument','date'])
df = df.unstack('port')
df['pchg',''] = xrange(25)

我该如何做相同的

df['pchg'] * df[['pf','bm']]

预计输出是一个与df具有相同索引且具有两列的数据帧。 df.pchg * df.bmdf.pchg * df.pf

由于

1 个答案:

答案 0 :(得分:2)

Pandas让这很简单。这是一个虚拟数据的例子:

df['data']['pf'] = df['data']['pf'] * df['pchg']
df['data']['bm'] = df['data']['bm'] * df['pchg']

在乘法之前:

                             data     pchg
port                           bm  pf     
sector instrument date                    
1      A          2013-01-14    0   0    0
                  2013-01-15    5   5    1
                  2013-01-16   10  10    2
                  2013-01-17   15  15    3
                  2013-01-18   20  20    4
       B          2013-01-14    1   1    5
                  2013-01-15    6   6    6
                  2013-01-16   11  11    7
                  2013-01-17   16  16    8
                  2013-01-18   21  21    9
       C          2013-01-14    2   2   10
                  2013-01-15    7   7   11
                  2013-01-16   12  12   12
                  2013-01-17   17  17   13
                  2013-01-18   22  22   14
2      D          2013-01-14    3   3   15
                  2013-01-15    8   8   16
                  2013-01-16   13  13   17
                  2013-01-17   18  18   18
                  2013-01-18   23  23   19
       E          2013-01-14    4   4   20
                  2013-01-15    9   9   21
                  2013-01-16   14  14   22
                  2013-01-17   19  19   23
                  2013-01-18   24  24   24

在乘法之后:

                             data      pchg
port                           bm   pf     
sector instrument date                     
1      A          2013-01-14    0    0    0
                  2013-01-15    5    5    1
                  2013-01-16   20   20    2
                  2013-01-17   45   45    3
                  2013-01-18   80   80    4
       B          2013-01-14    5    5    5
                  2013-01-15   36   36    6
                  2013-01-16   77   77    7
                  2013-01-17  128  128    8
                  2013-01-18  189  189    9
       C          2013-01-14   20   20   10
                  2013-01-15   77   77   11
                  2013-01-16  144  144   12
                  2013-01-17  221  221   13
                  2013-01-18  308  308   14
2      D          2013-01-14   45   45   15
                  2013-01-15  128  128   16
                  2013-01-16  221  221   17
                  2013-01-17  324  324   18
                  2013-01-18  437  437   19
       E          2013-01-14   80   80   20
                  2013-01-15  189  189   21
                  2013-01-16  308  308   22
                  2013-01-17  437  437   23
                  2013-01-18  576  576   24