简体   繁体   English

熊猫数据框分组:双分组和应用功能

[英]Pandas Dataframe groupby: double groupby & apply function

I have a question regarding pandas dataframes: 我对熊猫数据框有疑问:

I have a dataframe like the following, 我有一个如下数据框,

df = pd.DataFrame([[1,1,10],[1,1,30],[1,2,40],[2,3,50],[2,3,150],[2,4,100]],columns=["a","b","c"])   

   a  b    c
0  1  1   10
1  1  1   30
2  1  2   40
3  2  3   50
4  2  3  150
5  2  4  100

And i want to produce the following output, 我想产生以下输出,

  a "new col"
0 1 30
1 2 100

where the first line is calculated as the following: 第一行计算如下:

  1. Group df by the first column "a", 将df按第一列“ a”分组,
  2. Then group each grouped object the "b" 然后将每个分组的对象分组为“ b”
  3. calculate the mean of "c" for this b-group 计算此b组的“ c”平均值
  4. calculate the means of all b-groupbs for one "a" 计算一个“ a”的所有b-groupbs的均值
  5. this is the final value stored in "new col" for one "a" 这是存储在“ new col”中的一个“ a”的最终值

I can imagine that this is somehow confusing, but I hope this is understandable, nevertheless. 我可以想象这有点令人困惑,但是我希望这是可以理解的。

I achieved the desired result, but as i need it for a huge dataframe, my solution is probably much to slow, 我达到了预期的结果,但是由于需要一个庞大的数据框,因此我的解决方案可能要慢很多,

pd.DataFrame([ [a, adata.groupby("b").agg({"c": lambda x:x.mean()}).mean()[0]] for a,adata in df.groupby("a") ],columns=["a","new col"])
   a  new col
0  1     30.0
1  2    100.0

Therefore, what I would need is something like (?) df.groupby("a").groupby("b")["c"].mean() 因此,我需要的是(?)df.groupby(“ a”)。groupby(“ b”)[“ c”]。mean()

Thank you very much in advance! 提前非常感谢您!

Here's one way 这是一种方法

In [101]: (df.groupby(['a', 'b'], as_index=False)['c'].mean()
             .groupby('a', as_index=False)['c'].mean()
             .rename(columns={'c': 'new col'}))
Out[101]:
   a  new col
0  1       30
1  2      100
In [57]: df.groupby(['a','b'])['c'].mean().mean(level=0).reset_index()
Out[57]:
   a    c
0  1   30
1  2  100
df.groupby(['a','b']).mean().reset_index().groupby('a').mean()
Out[117]: 
     b      c
a            
1  1.5   30.0
2  3.5  100.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM