【问题标题】:Most efficient way to calculate the mean of a group of columns in a pandas DataFrame计算pandas DataFrame中一组列的平均值的最有效方法
【发布时间】:2012-07-01 04:21:39
【问题描述】:

我有一个DataFrame,其中的列如下:

["A_1", "A_2", "A_3", "B_1", "B_2", "B_3"]

我想将各个 A 列和 B 列“折叠”在一个列中并计算它们的平均值。简而言之,在操作结束时,我会得到:

["A", "B"]

其中“A”是所有“A”列的按列平均值,“B”是所有“B”列的平均值。

据我了解,groupby 不适合此任务,或者我使用不正确:

grouped = data.groupby([item for item in data if "A" not in item])

如果我使用axis=1,我在调用mean() 时得到的只是一个空的DataFrame,如果不是,我就得不到想要的效果。我想避免构建一个单独的 DataFrame 以通过迭代填充手段(例如,通过分别计算手段然后像new_df["A"] = mean_a 一样添加它们)。有没有有效的解决方案?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您想使用接受 axis 参数的内置 mean() 函数来指定逐行均值。由于您知道您想要的不同方式的特定列名称约定,因此您可以使用下面的示例代码来非常有效地执行此操作。在这里,我选择只添加两个额外的列,而不是实际销毁现有数据。我也可以将这些新列放入一个新的数据框中;这仅取决于您的需求以及对您来说方便的方式。无论哪种情况,相同的基本思想都适用。

    In [1]: import pandas
    
    In [2]: dfrm = pandas.DataFrame([[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18]], columns = ['A_1', 'A_2', 'A_3', 'B_1', 'B_2', 'B_3'])
    
    In [3]: dfrm
    Out[3]: 
       A_1  A_2  A_3  B_1  B_2  B_3
    0    1    2    3    4    5    6
    1    7    8    9   10   11   12
    2   13   14   15   16   17   18
    
    In [4]: dfrm["A_mean"] = dfrm[[elem for elem in dfrm.columns if elem[0]=='A']].mean(axis=1)
    
    In [5]: dfrm
    Out[5]: 
       A_1  A_2  A_3  B_1  B_2  B_3  A_mean
    0    1    2    3    4    5    6       2
    1    7    8    9   10   11   12       8
    2   13   14   15   16   17   18      14
    
    In [6]: dfrm["B_mean"] = dfrm[[elem for elem in dfrm.columns if elem[0]=='B']].mean(axis=1)
    
    In [7]: dfrm
    Out[7]: 
       A_1  A_2  A_3  B_1  B_2  B_3  A_mean  B_mean
    0    1    2    3    4    5    6       2       5
    1    7    8    9   10   11   12       8      11
    2   13   14   15   16   17   18      14      17
    

    【讨论】:

    • 我也会试试这个,看看这两个解决方案中哪一个最好,谢谢。
    【解决方案2】:

    我不知道效率,但我可能会这样做:

    ~/coding$ cat colgroup.dat
    A_1,A_2,A_3,B_1,B_2,B_3
    1,2,3,4,5,6
    7,8,9,10,11,12
    13,14,15,16,17,18
    ~/coding$ python
    Python 2.7.3 (default, Apr 20 2012, 22:44:07) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pandas
    >>> df = pandas.read_csv("colgroup.dat")
    >>> df
       A_1  A_2  A_3  B_1  B_2  B_3
    0    1    2    3    4    5    6
    1    7    8    9   10   11   12
    2   13   14   15   16   17   18
    >>> grouped = df.groupby(lambda x: x[0], axis=1)
    >>> for i, group in grouped:
    ...     print i, group
    ... 
    A    A_1  A_2  A_3
    0    1    2    3
    1    7    8    9
    2   13   14   15
    B    B_1  B_2  B_3
    0    4    5    6
    1   10   11   12
    2   16   17   18
    >>> grouped.mean()
    key_0   A   B
    0       2   5
    1       8  11
    2      14  17
    

    我想lambda x: x.split('_')[0] 会更健壮一些。

    【讨论】:

    • 从我做的初始测试开始,它似乎工作正常,我会在周一能够在真实数据上运行它时回来查看。
    • 对于我的真实数据(有几个组),两个不同的 groupby() 调用可以很好地解决问题,而另一个解决方案则稍微慢一些。
    猜你喜欢
    • 1970-01-01
    • 2016-10-24
    • 2019-09-04
    • 1970-01-01
    • 2022-01-07
    • 2022-11-12
    • 2015-07-22
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多