Derek Krantz
Derek Krantz

Reputation: 487

trouble accessing multi index column after join

I created the following multi-index column:

Out[213]:
    KEY POLL
        count   mean    sum
0   1   21  0.80921     10  
1   2   3   0.666667    2
2   3   67  0.835821    3
3   4   13  1.000000    4
4   5   674 0.876855    5

I can access the POLL multi-index column if i need to:

session_counts_merged[('POLL','sum')].head()
Out[225]:
0    0
1    0
2    0
3    0
4    0
Name: (POLL, sum), dtype: int64

However when I join the above table with another one, I can't figure out how to access the table anymore.

Here is the .info() on the new table:

account_aggregates.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 9713 entries, 0 to 9712
Data columns (total 6 columns):
NATIVEACCOUNTKEY           9713 non-null int64
(NATIVEACCOUNTKEY, )       9713 non-null int64
(POLL, count)              9713 non-null int64
(POLL, mean)               9713 non-null float64
(POLL, sum)                9713 non-null int64
session_deciles            9713 non-null object

How can i access the column named (POLL, sum)? Doing something like this:

account_aggregates_grouped['(POLL, sum)'].head()

results in a key not found error

Upvotes: 0

Views: 33

Answers (1)

unutbu
unutbu

Reputation: 880339

'(POLL, sum)' is a string. ('POLL','sum') is a tuple containing two strings. A DataFrame with a MultiIndex has keys which are tuples composed of a label from each index level. Therefore, change

account_aggregates_grouped['(POLL, sum)'].head()

to

account_aggregates_grouped[('POLL', 'sum')].head()

Upvotes: 1

Related Questions