在大 pandas 运作中是否与groupby互补(相反)? [英] Is there in pandas operation complementary (opposite) to groupby?

查看:71
本文介绍了在大 pandas 运作中是否与groupby互补(相反)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表(数据框)与许多列。现在我想在其中一个列中平均值。这意味着我需要对除了需要平均的列之外的所有列进行分组。当然我可以写:

  df.groupby(['col1','col2','col3','col4' ,'col5'])['vals']。mean()

但是,如果我可以这样做:

  df.groupby(['col6'],something ='reverse')['vals' ] .mean()

是否可能在大熊猫?

解决方案

您正在搜索您手头列表中的补充列。你可以玩 df.columns 。它代表一个索引对象,允许一些有趣的操作。



df.columns.drop (['col6'])返回一个索引,其中列表作为参数被删除。您可以将其转换为列表,并将其用作 groupby 参数:

  df.groupby(df.columns.drop(['col6'])。tolist())['vals']。mean()


I have a table (data frame) with many columns. Now I would like to average values in one of the columns. It means that I need to group by over all columns except the one over which I need to average. Of course I can write:

df.groupby(['col1', 'col2', 'col3', 'col4', 'col5'])['vals'].mean()

But it would be nice if I could do something like:

df.groupby(['col6'], something='reverse')['vals'].mean()

Is it possible in pandas?

解决方案

You are searching for the complementary columns to a list you have on hands. You can play with df.columns. It represents an Index object that allows some interesting manipulations.

df.columns.drop(['col6']) returns an Index with the list of columns passed as argument removed. You can convert it into a list and use it as the groupby argument:

df.groupby(df.columns.drop(['col6']).tolist())['vals'].mean()

这篇关于在大 pandas 运作中是否与groupby互补(相反)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆