python - How to extract from a multi-indexed pandas.Series object based on secondary level criterion? -


i have pandas dataframe sample object below on perform group-by correlation calculation. @ end view time-series correlation between value1 , value2

data = [ (1, 'alpha', 3, 101, 2), (1, 'beta', 2, 102, 3), (1, 'gamma', 5, 103, 4), (2, 'alpha', 2.5, 101, 1), (2, 'beta', 2.2, 105, 2), (2, 'gamma', 5, 100, 0), (3, 'alpha', 2.1, 102, 0), (3, 'beta', 2.0, 102, 3.3), (3, 'gamma', 5, 100, 2), ]  datapd = pandas.dataframe(data, columns=('time', 'id', 'value1', 'value2', 'value3')) corrvals = datapd.groupby('time').corr() 

so when @ corrvals['value1'], select value2 items. they're on level after time. e.g. corrvals['value1'].index.values shows:

array([(1, 'value1'), (1, 'value2'), (1, 'value3'), (2, 'value1'),        (2, 'value2'), (2, 'value3'), (3, 'value1'), (3, 'value2'),        (3, 'value3')], dtype=object) 

how ask values index value2 in 2nd tuple , no requirements on first?

you can use new indexslice:

in [17]: idx = pd.indexslice corrvals.loc[idx[:,'value2']]  out[17]: time         1     value1    0.654654       value2    1.000000       value3    1.000000 2     value1   -0.725288       value2    1.000000       value3    0.944911 3     value1   -0.999569       value2    1.000000       value3   -0.121560 name: value2, dtype: float64 

or slice:

in [18]: corrvals.loc[slice(none),'value2']  out[18]: time         1     value1    0.654654       value2    1.000000       value3    1.000000 2     value1   -0.725288       value2    1.000000       value3    0.944911 3     value1   -0.999569       value2    1.000000       value3   -0.121560 name: value2, dtype: float64 

or pass axis=0 loc:

in [19]: corrvals.loc(axis=0)[:,'value2']  out[19]:                value1  value2    value3 time                                    1    value2  0.654654       1  1.000000 2    value2 -0.725288       1  0.944911 3    value2 -0.999569       1 -0.121560 

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 -