Python pandas, how can I pass a colon ":" to an indexer through a variable -


i working through method working data slices large multi-index pandas dataframe. can generate masks use each indexer (essentially lists of values define slice):

df.loc[idx[a_mask,b_mask],idx[c_mask,d_mask]] 

this fine in scenarios i'd select along of axes, equivalent to:

df.loc[idx[a_mask,b_mask],idx[:,d_mask]] 

is there way me pass colon ":" replaces c_mask in second example in variable? ideally i'd set c_mask value ":", of course doesn't work (and shouldn't because if had column named that...). there way pass in value variable communicates "whole axis" along 1 of indexers?

i realize generate mask select gathering values along appropriate axis, nontrivial , adds lot of code. likewise break dataframe access 5 scenarios (one each having : in , 1 4 masks) doesn't seem honor dry principle , still brittle because can't handle multiple direction whole slice selection.

so, can pass in through variable select entire direction in indexer : would? or there more elegant way optionally select entire direction?

idx[slice(none)] equivalent idx[:]

so these equivalent.

in [11]: df = dataframe({'a' : np.random.randn(9)},index=pd.multiindex.from_product([range(3),list('abc')],names=['first','second']))  in [12]: df out[12]:                       first second           0          -0.668344       b      -1.679159       c       0.061876 1          -0.237272       b       0.136495       c      -1.296027 2           0.554533       b       0.433941       c      -0.014107  in [13]: idx = pd.indexslice  in [14]: df.loc[idx[:,'b'],] out[14]:                       first second           0     b      -1.679159 1     b       0.136495 2     b       0.433941  in [15]: df.loc[idx[slice(none),'b'],] out[15]:                       first second           0     b      -1.679159 1     b       0.136495 2     b       0.433941  in [16]: df.loc[(slice(none),'b'),] out[16]:                       first second           0     b      -1.679159 1     b       0.136495 2     b       0.433941 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -