【问题标题】:How to select by categorical index of grouped data using Pandas如何使用 Pandas 按分组数据的分类索引进行选择
【发布时间】:2016-12-17 05:55:42
【问题描述】:

我正在尝试从分组分类索引中选择数据,但无法按组选择。

这些数据是使用 pd.cut 在 60 个 bin 中收集的。最终目标是选择索引范围然后绘制数据 - 但是我无法选择数据。

例如 - 我想选择 (0,60] 和 (60,120] 的切片,但无法切片。非常感谢任何帮助。

当前数据框数据:

谢谢

【问题讨论】:

    标签: python pandas group-by slice categorical-data


    【解决方案1】:

    pd.cut 只是创建字符串值,因此您可以使用.ix 直接在列表中提供它们,或者使用.iloc 进行位置索引:

    In [7]: df
    Out[7]:
                0    1
    1
    (-1, 0]  0.00  0.0
    (0, 1]   0.01  1.0
    (1, 2]   0.02  2.0
    (2, 4]   0.03  3.0
    
    In [8]: df.ix[['(0, 1]', '(1, 2]']]
    Out[8]:
               0    1
    1
    (0, 1]  0.01  1.0
    (1, 2]  0.02  2.0
    
    In [9]: df.iloc[1:3]
    Out[9]:
               0    1
    1
    (0, 1]  0.01  1.0
    (1, 2]  0.02  2.0
    

    【讨论】:

    • 谢谢!我知道它非常简单,但语法不太正确。
    猜你喜欢
    • 2020-08-14
    • 2021-09-28
    • 1970-01-01
    • 2021-10-10
    • 2019-05-24
    • 1970-01-01
    • 2020-02-12
    • 2020-08-07
    • 1970-01-01
    相关资源
    最近更新 更多