Reputation: 19800
I'm struggling to access a Pandas DataFrame with a multi-index programatically. Let's say I have
import pandas as pd
df = pd.DataFrame([[0, 0, 0, 1],
[0, 0, 1, 2],
[0, 1, 0, 7],
[0, 1, 1, 9],
[1, 0, 0, 1],
[1, 0, 1, 0],
[1, 1, 0, 1],
[1, 1, 1, 10]], columns=['c1', 'c2', 'c3', 'value'])
sums = df.groupby(['c1', 'c2', 'c3']).value.sum()
I can get the sum which corresponds to the [1, 1, 1] combination of c1, c2 and c3 with
sums[1, 1, 1]
That returns 10 as expected.
But what if I have a variable
q = [1, 1, 1]
how do I get the same value out?
I have tried
sums[q]
which gives
c1 c2 c3
0 0 1 2
1 2
1 2
Name: value, dtype: int64
Also I thought star operator could work:
sums[*q]
but that is invalid syntax.
Upvotes: 2
Views: 333
Reputation: 863146
Use Series.xs
with tuple
:
print (sums.xs((1,1,1)))
10
Or Series.loc
:
print (sums.loc[(1,1,1)])
#alternative
#print (sums[(1,1,1)])
10
q = [1, 1, 1]
print (sums.loc[tuple(q)])
#alternative
#print (sums[tuple(q)])
10
Upvotes: 1