pandas横截面索引-选择多列

问题描述 投票:0回答:2

在对熊猫进行多重索引切片时如何选择count50%

import pandas as pd

df = pd.DataFrame({
    'foo': [1,2,3], 'bar':[4,5,6], 'dt':['2020-01-01', '2020-01-01', '2020-01-02']
})
df.groupby(['dt']).describe().loc[:, (slice(None), '50%'), (slice(None), 'count')]

失败:

IndexingError: Too many indexers
python pandas
2个回答
1
投票

另一种鲜为人知的方法是将axis参数用于.locsee docs

df = pd.DataFrame({
    'foo': [1,2,3], 'bar':[4,5,6], 'dt':['2020-01-01', '2020-01-01', '2020-01-02']
})
df.groupby(['dt']).describe().loc(axis=1)[:, ['count','50%']]

输出:

             foo        bar     
           count  50% count  50%
dt                              
2020-01-01   2.0  1.5   2.0  4.5
2020-01-02   1.0  3.0   1.0  6.0

2
投票

使用pd.IndexSlice

ix = pd.IndexSlice
df.groupby(['dt']).describe().loc[:, ix[:, ['count', '50%']]]

Out[8]:
             bar        foo
           count  50% count  50%
dt
2020-01-01   2.0  4.5   2.0  1.5
2020-01-02   1.0  6.0   1.0  3.0
© www.soinside.com 2019 - 2024. All rights reserved.