Is there a way to get the result of get_level_values
for more than one column?
Given the following DataFrame
:
d
a b c
1 4 10 16
11 17
5 12 18
2 5 13 19
6 14 20
3 7 15 21
I wish to get the values (i.e. list of tuples) of levels a
and c
:
[(1, 10), (1, 11), (1, 12), (2, 13), (2, 14), (3, 15)]
Notes:
It is impossible to give
get_level_values
more than one level (e.g.df.index.get_level_values(['a','c']
)There's a workaround in which one could use
get_level_values
over each desired column andzip
them together:
For example:
a_list = df.index.get_level_values('a').values
c_list = df.index.get_level_values('c').values
print([i for i in zip(a_list,c_list)])
[(1, 10), (1, 11), (1, 12), (2, 13), (2, 14), (3, 15)]
but it get cumbersome as the number of columns grow.
- The code to build the example
DataFrame
:
df = pd.DataFrame({'a':[1,1,1,2,2,3],'b':[4,4,5,5,6,7,],'c':[10,11,12,13,14,15], 'd':[16,17,18,19,20,21]}).set_index(['a','b','c'])