Python 多索引系列中的自定义聚合

Python 多索引系列中的自定义聚合,python,pandas,Python,Pandas,如何替换中的列 import numpy as np import pandas as pd arrays = [np.array(['bar', 'bar', 'bar','baz', 'baz','baz', 'foo', 'foo','foo']), np.array(['one', 'two', 'three', 'one', 'two','three', 'one', 'two','three'])] s = pd.Series(np.random.randn(9)

如何替换中的列

import numpy as np
import pandas as pd
arrays = [np.array(['bar', 'bar', 'bar','baz', 'baz','baz', 'foo', 'foo','foo']),
          np.array(['one', 'two', 'three', 'one', 'two','three', 'one', 'two','three'])]
s = pd.Series(np.random.randn(9), index=arrays)
print(s)

bar  one      0.791608
     two     -0.966179
     three    0.320251
baz  one      0.043479
     two     -1.637586
     three   -1.133128
foo  one     -0.575991
     two     -1.080433
     three    0.946663
通过包含自定义聚合结果的列,例如

(第三次进入-第一次进入)/第一次进入

对于每个一级索引组

即,“bar”的列值将是

(0.320251-0.791608)/0.791608
生成的系列应该像这样打印

bar  -0.5954424412
baz  ...
foo  ...

groupby
之后使用
first
last
,也可以使用
nth

g=s.groupby(level=0)
(g.last()-g.first())/g.first()
Out[132]: 
bar   -0.818922
baz   -0.150440
foo    0.266949
dtype: float64
或者只是切片

(s.loc[:,'three']-
   s.loc[:,'one'])/s.loc[:,'one']
Out[135]: 
bar   -0.818922
baz   -0.150440
foo    0.266949
dtype: float64

切片法不错+1.