在多索引级别对pandas .diff()进行分区 [英] Partition pandas .diff() in multi-index level

查看:168
本文介绍了在多索引级别对pandas .diff()进行分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题涉及在多索引级别的分区中调用.diff()

My question relates to calling .diff() within the partition of a multi index level

在下面的示例中,第一个

In the following sample the output of the first

df.diff()是

df.diff() is

               values
Greek English        
alpha a           NaN
      b             2
      c             2
      d             2
beta  e            11
      f             1
      g             1
      h             1

但我希望它是

               values
Greek English        
alpha a           NaN
      b             2
      c             2
      d             2
beta  e            NaN
      f             1
      g             1
      h             1

这是一个使用循环的解决方案,但我认为我可以避免该循环

Here is a solution, using a loop but I am thinking I can avoid that loop!

import pandas as pd
import numpy as 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.) This is not the type of .diff() i want.
# I need it to respect the level='Greek' and restart   
print df.diff()


# this is one way to achieve my desired result but i have to think
# there is a way that does not involve the need to loop.
idx = pd.IndexSlice
for 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 by level=0 or 'Greek' if you prefer and then you can call diff on values:

In [179]:

df.groupby(level=0)['values'].diff()
Out[179]:
Greek  English
alpha  a         NaN
       b           2
       c           2
       d           2
beta   e         NaN
       f           1
       g           1
       h           1
dtype: float64

这篇关于在多索引级别对pandas .diff()进行分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆