简体   繁体   中英

Slicing Multiindex Data Frame with zip values as index

I have a Multiindex dataframe like this:

import pandas as pd
import numpy as np

lat = [10.1,16.5,35.8,40.6,80.7,60.1]
long = [11.3,19.4,33.6,43.8,65.2,60.1]

years = list(range(2000,2010,1))
months = list(range(1,13,1))
variables = ['S','L','A','W']

miindex = pd.MultiIndex.from_product([zip(lat,long),years, months])
micolumns = pd.MultiIndex.from_tuples(variables)
dfmi = pd.DataFrame(np.arange(len(miindex) * len(micolumns)).reshape((len(miindex),\
                              len(micolumns))),index=miindex,    \
                              columns=micolumns).                \
                              sort_index().sort_index(axis=0)

#In [10]: dfmi
#Out[10]:
#                         S     L     A     W
#(10.1, 11.3) 2000 1      0     1     2     3
#                  2      4     5     6     7
#                  3      8     9    10    11
#                  4     12    13    14    15
#                  5     16    17    18    19
#                  6     20    21    22    23
#                  7     24    25    26    27
#                  8     28    29    30    31
#                  9     32    33    34    35
#                  10    36    37    38    39
#                  11    40    41    42    43
#                  12    44    45    46    47
#             2001 1     48    49    50    51
#                  2     52    53    54    55
#                  3     56    57    58    59

I'm able to slice this dataset depending upon years or months like this

summer_data = dfmi.loc[(slice(None),slice(None),slice(6,8)),:]
#In[11]: summer_data
#Out[11]:
#                        S     L     A     W
#(10.1, 11.3) 2000 6    20    21    22    23
#                  7    24    25    26    27
#                  8    28    29    30    31
#             2001 6    68    69    70    71
#                  7    72    73    74    75
#                  8    76    77    78    79
#             2002 6   116   117   118   119
#                  7   120   121   122   123

I want to slice this dataset depending upon latitude values like greater than 30. I have gone through from pandas MultiIndex / Advanced Indexing documentation but I didn't find any thing helpful.Is there anyway by which I can do this?

In guess in general it's not as good idea to use a tupel in a multiindex. The multiindex itself is a tuple and you will run into troubles. But why do you combine lat and long ? If you use

miindex = pd.MultiIndex.from_product([lat,long,years, months], names=['lat', 'long', 'year', 'month'])

you can write this:

dfmi.loc[(slice(30, None), slice(None), slice(None), slice(None)), :]

or

dfmi.query("lat > 30")

Here you can find a really good overview about slicing a multiindex.

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