简体   繁体   English

在 pandas 中的 groupby 上创建垃圾箱

[英]Create bins on groupby in pandas

my data frame got a column department and rank我的数据框有一个列departmentrank

name  dept  Rank
"A"  "ENG"  1
"A"  "MGMT"  1
"B"  "ENG"  2
"C"  "MGMT"  2
"D"  "MGMT"  3
"E"  "ENG"  3

I want to create bins in each dept .我想在每个dept创建垃圾箱。

name  dept  Rank  Comment
"A"  "ENG"  1  Good
"A"  "MGMT"  1  Good
"B"  "ENG"  2  Avg
"C"  "MGMT"  2  Avg
"D"  "MGMT"  3  Poor
"E"  "ENG"  3  Poor

I'm able to do this by below code我可以通过下面的代码做到这一点

 df['Comment'] = pd.qcut(df[df['dept'] == "ENG"]['Rank'], q=[0.0, .25, .5, 1.0], labels=['Good', 'Avg', 'Poor'])
 df['Comment'] = pd.qcut(df[df['dept'] == "MGMT"]['Rank'], q=[0.0, .25, .5, 1.0], labels=['Good', 'Avg', 'Poor'])

I'm very sure, there must be some way using groupby , but can't figure it out.我很确定,一定有某种使用groupby的方法,但无法弄清楚。

wouldn't it be simpler to create a dictionary and map the values?创建字典和 map 值不是更简单吗? Since you would need to do it anyways while creating the labels and mapping to the bins因为在创建标签和映射到垃圾箱时无论如何都需要这样做

d={1:'Good',
   2:'Avg',
   3:'Poor'}

df['comments']=df['Rank'].map(d)
df
    name    dept    Rank    comments
0   "A"     "ENG"     1     Good
1   "A"     "MGMT"    1     Good
2   "B"     "ENG"     2     Avg
3   "C"     "MGMT"    2     Avg
4   "D"     "MGMT"    3     Poor
5   "E"     "ENG"     3     Poor

You can use groupby.transform / groupby.apply :您可以使用groupby.transform / groupby.apply

df['Comment'] = (df
 .groupby('dept')['Rank']
 .transform(lambda g:
     pd.qcut(g,
             q=[0, 1/3, 2/3, 3/3],   # changed
             labels=['Good', 'Avg', 'Poor']))
 )

NB.注意。 quantiles are in the range [0, 1], I changed the example.分位数在 [0, 1] 范围内,我更改了示例。

Output: Output:

  name  dept  Rank Comment
0    A   ENG     1    Good
1    A  MGMT     1    Good
2    B   ENG     2     Avg
3    C  MGMT     2     Avg
4    D  MGMT     3    Poor
5    E   ENG     3    Poor

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

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