1

I am creating a multi indexed series like this :

  index = [ ('America','California', 2000), ('America','California', 2010),
          ('Asia','Delhi', 2000), ('Asia','Delhi', 2010),
          ('Asia','HK', 2000), ('Asia','HK', 2010),
          ('America','Texas', 2000), ('America','Texas', 2010)
        ]

populations = [33871648, 37253956,18976457, 19378102,30861820, 35145561,20851820, 25145561]

new_index =  pd.MultiIndex.from_tuples(index)
pop = pd.Series(populations, index=new_index).sort_index()

I am trying to retrieve values like this :

pop['America'] # works
pop['America','Texas',2010] # works
pop['America','Texas'] # works

How to get for america, any city, but 2010?

pop['America',:,2010] # not working

How to use slicing in situations like this ?

Abhishek Sengupta
  • 2,938
  • 1
  • 28
  • 35

1 Answers1

1

Use:

idx = pd.IndexSlice
print (pop.loc[idx['America',:,2010]])
America  California  2010    37253956
         Texas       2010    25145561
dtype: int64

Or:

print (pop.loc['America',slice(None),2010])
America  California  2010    37253956
         Texas       2010    25145561
dtype: int64
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252