简体   繁体   中英

python pandas multiindex subtract rows with matching level 1 index

pandas DataFrame:

Constructor:

iterables = [[date(2018,5,31),date(2018,6,26),date(2018,6,29),date(2018,7,1)], 
['test1','test2']]
indx = pd.MultiIndex.from_product(iterables, names=['date','tests'])
col = ['tests_passing', 'tests_total']
data = np.array([[834,3476],[229,256],[1524,1738],[78,144],[1595,1738],[78,144],[1595,1738],[142,144]])
df = pd.DataFrame(data, index=indx, columns=col)
df = df.assign(tests_remaining= df['tests_total'] - df['tests_passing'])

Dataframe:

                 tests_passing  tests_total  tests_remaining
date       tests                                             
2018-05-31 test1            834         3476             2642
           test2            229          256               27
2018-06-26 test1           1524         1738              214
           test2             78          144               66
2018-06-29 test1           1595         1738              143
           test2             78          144               66
2018-07-01 test1           1595         1738              143
           test2            142          144                2

This data consists of a number of test measurements (test1,test2,...,etc) each collected on some date. I want to create a new column in this dataframe named 'progress' which would in general select all rows where test = unique test (test1 for example) across all dates and subtract the 'tests_remaining' column value for that row at date0 with the next value for row at date1,date2,...,etc so basically: df.loc[(date0,test0),'progress'] = df.loc[(date0,test0),'tests_remaining']-df.loc[(date1,test0),'tests_remaining] (with the one exception that the first date would have a progress value of 0 since it was the first collected date).

The desired output will look like this:

                 tests_passing  tests_total  tests_remaining  progress
date      tests                                                       
5/31/2018 test1            834         3476             2642         0
          test2            229          256               27         0
6/26/2018 test1           1524         1738              214      2428
          test2             78          144               66       -39
6/29/2018 test1           1595         1738              143        71
          test2             78          144               66         0
7/1/2018  test1           1595         1738              143         0
          test2            142          144                2        64

So far I have been able to use loc[] with slices to select a single test at a time and perform this calculation as a resultant pandas Series, but I am unable to do this in general across all tests without specifying the test name explicitly in the split. This is not a reasonable solution for me as in the real data there are hundreds of tests.

All = slice(None)
df_slice = df.loc[(All,'test1'),'tests_remaining']
sub = df_slice.diff(periods=-1).shift(1).fillna(0);sub

date        tests
2018-05-31  test1       0.0
2018-06-26  test1    2428.0
2018-06-29  test1      71.0
2018-07-01  test1       0.0
Name: tests_remaining, dtype: float64

Is there a more pandas idiomatic way to create the desired column as described?

Thanks in advance for your help!

You can groupby level test and do diff

df.groupby(level='tests').tests_remaining.diff().mul(-1)
Out[662]: 
date        tests
2018-05-31  test1       NaN
            test2       NaN
2018-06-26  test1    2428.0
            test2     -39.0
2018-06-29  test1      71.0
            test2      -0.0
2018-07-01  test1      -0.0
            test2      64.0
Name: tests_remaining, dtype: float64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM