简体   繁体   中英

Slice pandas.DataFrame's second Multiindex

I have a pandas Dataframe of the form

              "a"          "b"          "c"        #first level index
            0, 1, 2       0, 1, 2      0, 1, 2     #second level index
index
0          1,2,3         6,7,8       5,3,4
1          2,3,4         7,5,4       9,2,5
2          3,4,5         4,5,6       0,4,5
...

representing a spot (a,b or c) where a measurement took place and the results of the measurments (0,1,2) that took place on this spot.

I want to do the following:

  • pick a slice in the sample (say the first measurement on each spot at measurement 0)
  • mean each i-th measurement (mean("a"[0], "b"[0], "c"[0]), mean("a"[1], "b"[1], "c"[1]), ...)

I tried to get the hang of the pandas Multiindex documentation but do not manage to slice for the second level.

This is the column index:

MultiIndex(levels=[['a', 'b', 'c', ... , 'y'], [0, 1, 2, ... , 49]],
       labels=[[0, 0, 0, ... , 0, 1, 1, 1, ... 1, ..., 49, 49, 49, ... 49]])

And the index

Float64Index([204.477752686, 204.484664917, 204.491577148,  ..., 868.723022461], dtype='float64', name='wavelength', length=43274)

Using

df[:][0]

yields a key-error (0 not in index)

df.iloc[0]

returns the horizontal slice

0    "a":(1,2,3), "b":(6,7,8), "c":(5,3,4)

but I would like to have

"a":(1,2,3), "b":(6,7,4), "c":(5,9,0)

THX for any help

PS: version:pandas-0.19, python-3.4

The trick was to specify the axis...

df.loc(axis=1)[:,0]

provides the 0-th measurment of each spot. Since I use integers on the second level index, I am not sure if this actually yields the label "0" or just the 0-th measurment in the DataFrame, label-agnostic.

But for my use-case, this is actually sufficient.

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