按行名分组Pandas DataFrame

0 投票
1 回答
5195 浏览
提问于 2025-04-18 01:10

我有一个简单的Pandas数据框,里面有行名和两列,像下面这样。

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)})

就像上面的例子一样,有些行名是重复的。我想根据行名来对我的数据框进行分组,这样我就可以对每个组进行一些汇总操作,比如计数或者求平均值。

举个例子,我可能想知道在我的数据框中,row1row4各出现了一次,而row2也只出现了一次。

我知道有一个叫groupby的方法,但我在网上看到的例子都是根据列的值来分组,而不是行名。真的是这样吗?我是不是应该把行名变成数据框中的一列呢?

1 个回答

2

查看文档说明(如果你在使用 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]

撰写回答