简体   繁体   中英

Assign to MultiIndex slice

In [332]: midx = MultiIndex(levels=[['zero', 'one'], ['x','y']],
   .....:                   labels=[[1,1,0,0],[1,0,1,0]])
   .....: 

In [333]: df = DataFrame(randn(4,2), index=midx)

In [334]: print(df)
               0         1
one  y  0.313092 -0.588491
     x  0.203166  1.632996
zero y -0.557549  0.126204
     x  1.643615 -0.067716

I want to set elements in column 0 where level 1 == y to nan. With .xs() I would do: df.xs('y', level=1)[0] = np.nan Which obviously doesn't work. Something along the lines of df.ix[(:, 'y'), 0] = np.nan which of course is also not valid.

In <= 0.13.1

In [51]: df = DataFrame(randn(4,2), index=midx)

In [52]: df.loc[df.index.get_loc_level('y',level=1)[0],0] = np.nan

In [53]: df
Out[53]: 
               0         1
one  y       NaN -0.260289
     x  0.122913  0.728180
zero y       NaN -0.010145
     x -0.532615  0.758914

[4 rows x 2 columns]

In 0.14, you will be able to use a new syntax, see this: https://github.com/pydata/pandas/pull/6134

Something like:

df.loc[(slice(None),'y'),0] = np.nan

should work.

There's an open Github issue about improving the MultiIndex slice syntax, but for now I'd recommend:

In [59]: idx_slice = [(x, 'y') for x in df.index.levels[0]]

In [60]: idx_slice
Out[60]: [('zero', 'y'), ('one', 'y')]

In [62]: df.ix[idx_slice, 0] = np.nan

In [63]: df
Out[63]: 
               0         1
one  y       NaN  0.210371
     x -1.109476  1.861331
zero y       NaN  0.189710
     x -1.013922 -1.465135

[4 rows x 2 columns]

Hopefully that works for your real dataset!

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