pandas -使用前一列的汇总创建一个新列 [英] Pandas - Create a new column with aggregation of previous column

查看:103
本文介绍了 pandas -使用前一列的汇总创建一个新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含2列的数据框:

I have a dataframe with 2 columns:

CLASS   STUDENT
'Sci'   'Francy'
'math'  'Alex'
'math'  'Arthur'
'math'  'Katy'
'eng'   'Jack'
'eng'   'Paul'
'eng'   'Francy'

我想为数学"课程中的所有学生添加一个新列

I want to add a new column with all the students in the class 'math'

CLASS   STUDENT  NEW_COL
'Sci'   'Francy'   NaN
'math'  'Alex'    'Alex', 'Arthur, Katy'
'math'  'Arthur'  'Alex', 'Arthur, Katy'
'math'  'Katy'    'Alex', 'Arthur, Katy'
'eng'   'Jack'     NaN
'eng'   'Paul'     NaN
'eng'   'Francy'   NaN

我一直在尝试这样的事情,但是我走得还不是很远:

I have been trying something like this but I am not getting very far :

def get_all_students(class_series, df):
    return df.groupby(['CLASS','STUDENT']).size().rest_index()['CLASS'== measurement].tolist()
    ...

df['NEW_COL'] = np.where(df['CLASS']=='math', get_all_students(df['CLASS'],df),np.NaN)

推荐答案

IIUC使用条件分配groupby + transform

IIUC Using condition assign with the groupby + transform

df.loc[df.CLASS=='math','New']=df.groupby('CLASS').STUDENT.transform(','.join)
df
Out[290]: 
  CLASS STUDENT               New
0   Sci  Francy               NaN
1  math    Alex  Alex,Arthur,Katy
2  math  Arthur  Alex,Arthur,Katy
3  math    Katy  Alex,Arthur,Katy
4   eng    Jack               NaN
5   eng    Paul               NaN
6   eng  Francy               NaN

更多信息,由于我通过groupby计算所有组,因此您可以全部分配它们,也可以只选择需要的条件分配

More info, since I compute all the group by groupby , so that you can assign them all or just pick what you need conditional assign

df.groupby('CLASS').STUDENT.transform(','.join)
Out[291]: 
0              Francy
1    Alex,Arthur,Katy
2    Alex,Arthur,Katy
3    Alex,Arthur,Katy
4    Jack,Paul,Francy
5    Jack,Paul,Francy
6    Jack,Paul,Francy
Name: STUDENT, dtype: object

这篇关于 pandas -使用前一列的汇总创建一个新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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