【问题标题】:Group Pandas DataFrame by row name按行名对 Pandas DataFrame 进行分组
【发布时间】:2014-05-15 10:05:57
【问题描述】:

我有一个带有行名和 2 列的简单 Pandas DataFrame,如下所示。

from pandas import DataFrame, Series
row_names = ['row1', 'row2', 'row2', 'row4']
df = DataFrame({'col1': Series([1, 2, 3, 4], index=row_names),
                'col2': Series([0, 1, 0, 1], index=row_names)})

与上面的示例一样,某些行名重复。我想按行名对我的 DataFrame 进行分组,以便我可以按组执行聚合操作(例如,计数、平均值)。

例如,我可能想知道row1row4 在我的df 中各出现一次,而row2 出现一次。

我知道groupby 方法,但从我在网上看到的示例中,它只按列值分组,而不是按行名分组。是这样吗?我应该将我的行名设置为 DataFrame 中的一列吗?

【问题讨论】:

    标签: python pandas dataframe row pandas-groupby


    【解决方案1】:

    检查文档字符串(如果您使用的是IPython,那就是df.groupby?<enter>

    Group series using mapper (dict or key function, apply given function
    to group, return result as series) or by a series of columns
    
    Parameters
    ----------
    by : mapping function / list of functions, dict, Series, or tuple /
        list of column names.
        Called on each element of the object index to determine the groups.
        If a dict or Series is passed, the Series or dict VALUES will be
        used to determine the groups
    axis : int, default 0
    level : int, level name, or sequence of such, default None
        If the axis is a MultiIndex (hierarchical), group by a particular
        level or levels
    ...
    

    你想要level 参数:

    In [20]: df.groupby(level=0).count()
    Out[20]: 
          col1  col2
    row1     1     1
    row2     2     2
    row4     1     1
    
    [3 rows x 2 columns]
    

    【讨论】:

      猜你喜欢
      • 2013-12-06
      • 2019-04-10
      • 2022-08-15
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 2016-09-14
      • 2021-11-16
      • 2017-08-26
      相关资源
      最近更新 更多