Pandas MultiIndex Lookup By Equality and Set Membership
Question:
Given a pandas Series
, or Dataframe
, with a multiindex:
first_key = ['a', 'b', 'c']
second_key = [1, 2, 3]
m_index = pd.MultiIndex.from_product([first_key, second_key],
names=['first_key', 'second_key'])
series_with_index = pd.Series(0.0, index=m_index)
How can the MultiIndex be indexed to lookup an equality for the first level and an isin
on the second index?
For example, how can all values where the first level is equal to a
and the second level is in the set {2, 3, 4}
be set to 1.0
?
Thank you in advance for your consideration and response.
Answers:
Try this:
you can use index.get_level_values()
to find all the values in the first level that equal a
.
index.isin()
has a level parameter, so you can pass your set into that.
lastly change the values in the series to 1 where they are both True
m1 = series_with_index.index.get_level_values(0) == 'a'
m2 = series_with_index.index.isin({2,3,4},level=1)
series_with_index.mask(m1 & m2,1)
Output:
first_key second_key
a 1 0.0
2 1.0
3 1.0
b 1 0.0
2 0.0
3 0.0
c 1 0.0
2 0.0
3 0.0
With composite conditions for each of 2 levels:
s = pd.Series(0.0, index=m_index)
s[(s.index.get_level_values(0) == 'a') & ((s.index.get_level_values(1).isin({2, 3, 4})))] = 1.0
first_key second_key
a 1 0.0
2 1.0
3 1.0
b 1 0.0
2 0.0
3 0.0
c 1 0.0
2 0.0
3 0.0
You can use get_level_values
and perform boolean indexing:
m1 = series_with_index.index.get_level_values('first_key')=='a'
m2 = series_with_index.index.get_level_values('second_key').isin([2,3,4])
series_with_index[m1&m2] = 1
Updated Series:
first_key second_key
a 1 0.0
2 1.0
3 1.0
b 1 0.0
2 0.0
3 0.0
c 1 0.0
2 0.0
3 0.0
dtype: float64
query
works with named index, but only available for DataFrame:
my_set = {2, 3, 4}
series_with_index.to_frame()
.query('first_key=="a" & second_key in @my_set')
.iloc[:,0]
Given a pandas Series
, or Dataframe
, with a multiindex:
first_key = ['a', 'b', 'c']
second_key = [1, 2, 3]
m_index = pd.MultiIndex.from_product([first_key, second_key],
names=['first_key', 'second_key'])
series_with_index = pd.Series(0.0, index=m_index)
How can the MultiIndex be indexed to lookup an equality for the first level and an isin
on the second index?
For example, how can all values where the first level is equal to a
and the second level is in the set {2, 3, 4}
be set to 1.0
?
Thank you in advance for your consideration and response.
Try this:
you can use index.get_level_values()
to find all the values in the first level that equal a
.
index.isin()
has a level parameter, so you can pass your set into that.
lastly change the values in the series to 1 where they are both True
m1 = series_with_index.index.get_level_values(0) == 'a'
m2 = series_with_index.index.isin({2,3,4},level=1)
series_with_index.mask(m1 & m2,1)
Output:
first_key second_key
a 1 0.0
2 1.0
3 1.0
b 1 0.0
2 0.0
3 0.0
c 1 0.0
2 0.0
3 0.0
With composite conditions for each of 2 levels:
s = pd.Series(0.0, index=m_index)
s[(s.index.get_level_values(0) == 'a') & ((s.index.get_level_values(1).isin({2, 3, 4})))] = 1.0
first_key second_key
a 1 0.0
2 1.0
3 1.0
b 1 0.0
2 0.0
3 0.0
c 1 0.0
2 0.0
3 0.0
You can use get_level_values
and perform boolean indexing:
m1 = series_with_index.index.get_level_values('first_key')=='a'
m2 = series_with_index.index.get_level_values('second_key').isin([2,3,4])
series_with_index[m1&m2] = 1
Updated Series:
first_key second_key
a 1 0.0
2 1.0
3 1.0
b 1 0.0
2 0.0
3 0.0
c 1 0.0
2 0.0
3 0.0
dtype: float64
query
works with named index, but only available for DataFrame:
my_set = {2, 3, 4}
series_with_index.to_frame()
.query('first_key=="a" & second_key in @my_set')
.iloc[:,0]