Partition pandas .diff() in multi-index level -
my question relates calling .diff() within partition of multi index level
in following sample output of first
df.diff()
values greek english alpha nan b 2 c 2 d 2 beta e 11 f 1 g 1 h 1
but want be:
values greek english alpha nan b 2 c 2 d 2 beta e nan f 1 g 1 h 1
here solution, using loop thinking can avoid loop!
import pandas pd import numpy np df = pd.dataframe({'values' : [1.,3.,5.,7.,18.,19.,20.,21.], 'greek' : ['alpha', 'alpha', 'alpha', 'alpha','beta','beta','beta','beta'], 'english' : ['a', 'b', 'c', 'd','e','f','g','h']}) df.set_index(['greek','english'],inplace =true) print df # (1.) not type of .diff() want. # need respect level='greek' , restart print df.diff() # 1 way achieve desired result have think # there way not involve need loop. idx = pd.indexslice greek_letter in df.index.get_level_values('greek').unique(): df.loc[idx[greek_letter,:]]['values'] = df.loc[idx[greek_letter,:]].diff() print df
just groupby
level=0
or 'greek' if prefer , can call diff
on values:
in [179]: df.groupby(level=0)['values'].diff() out[179]: greek english alpha nan b 2 c 2 d 2 beta e nan f 1 g 1 h 1 dtype: float64
Comments
Post a Comment