分组 pandas 数据框并将多个值收集到集合中

Grouping pandas dataframe and collecting multiple values into sets

假设我有以下数据框df1:

     A    B  C   D 
0  foo  one  1  0
1  bar  two  2  1
2  foo  two  3  0
3  bar  two  4  1
4  foo  two  5  0
5  bar  two  6  1
6  foo  one  7  0
7  foo  two  8  1

我想把它变成一个数据框 df2 像这样:

A     B            C                 D             
foo  [one,two]  [1,3,5,7,8]          0
bar  [two]          [2,4,6]          1

更准确地说:

我希望像 la df1.groupby("A").aggregate_like_this() 那样有一个单行聚合,但到目前为止我还没有找到它。

使用groupby + agg:

f = {'B' : lambda x: np.unique(x).tolist(), 
     'C' : lambda x: np.unique(x).tolist(), 
     'D' : 'first'
}

df.groupby('A', as_index=False).agg(f).reindex(columns=df.columns)

     A           B                C  D
0  bar       [two]        [2, 4, 6]  1
1  foo  [one, two]  [1, 3, 5, 7, 8]  0 

如果您无法预先确定 A 的哪些值与 D 有 1:1 关系,请使用 groupby + nunique 检查,然后相应地过滤您的数据集。

x = df.groupby('A').D.nunique().eq(1)
df = df[df.A.isin(x[x].index)]
df

     A    B  C  D
1  bar  two  2  1
3  bar  two  4  1
5  bar  two  6  1

df.groupby('A', as_index=False).agg(f).reindex(columns=df.columns)

     A      B          C  D
0  bar  [two]  [2, 4, 6]  1