如何执行 'group by' 然后将每个组附加到 pandas 中的相同行

How to perform 'group by' then append each group with same rows in pandas

首先,我需要使用来自 'set' 列的唯一值过滤/分组行,然后为每个过滤器/组创建相同的 2 个新行

获取每个组的名称并将其传递给列 'set' 值

也很重要

'set' : name,

数据

df = pd.DataFrame(np.array([[1, 2, 1, 'yes'], [1, 1, 0, 'no'],[1, 3, 0, 'no'],[2, 1, 0,'no'],[2, 2, 0,'yes'], [2, 3, 1,'no'], [3, 3, 1,'no'],[3, 1, 1,'no'],[3, 2, 1, 'no']]),
                   columns=['a', 'b', 'c', 'set'])
df

groups = df.groupby("set") 
for name, group in groups: 
        
    group = group.append({'a' : 12 , 'b' : 7, 'set' : name, }, ignore_index=True)
    group = group.append({'a' : 12 , 'b' : 8, 'set' : name, },  ignore_index=True)

     

尝试使用 apply:

def addRows(x):
     # <new_rows> write your values 
     x = x.append(new_rows)

df.groupby('set').apply(addRows).reset_index(drop=True)

编辑:

def addRows(x):
    set_name = (x.name) # <--- this will give you name.
    x = x.append({'a' : 12 , 'b' : 7, 'set' : set_name, }, ignore_index=True)
    x = x.append({'a' : 12 , 'b' : 8, 'set' : set_name, },  ignore_index=True)
    return x # <--- need to return


df.groupby('set').apply(addRows).reset_index(drop=True)

    a   b   c   set
0   1   1   0   no
1   1   3   0   no
2   2   1   0   no
3   2   3   1   no
4   3   3   1   no
5   3   1   1   no
6   3   2   1   no
7   12  7   NaN no
8   12  8   NaN no
9   1   2   1   yes
10  2   2   0   yes
11  12  7   NaN yes
12  12  8   NaN yes