【问题标题】:trouble accessing multi index column after join连接后访问多索引列时遇到问题
【发布时间】:2015-08-14 22:19:15
【问题描述】:

我创建了以下多索引列:

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

如果需要,我可以访问 POLL 多索引列:

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

但是,当我将上面的表与另一个表连接时,我无法弄清楚如何访问该表。

这是新表上的 .info():

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

如何访问名为 (POLL, sum) 的列?做这样的事情:

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

导致找不到密钥错误

【问题讨论】:

    标签: python pandas multi-index


    【解决方案1】:

    '(POLL, sum)' 是一个字符串。 ('POLL','sum') 是一个包含两个字符串的元组。 具有 MultiIndex 的 DataFrame 具有由标签组成的 元组 的键 从每个索引级别。因此,改变

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      • 2020-12-11
      • 2011-07-13
      • 1970-01-01
      • 2022-11-18
      • 2022-07-14
      相关资源
      最近更新 更多