小编Nit*_*ynx的帖子

使用 Pandas GroupBy 从多列聚合唯一值

我进入了无数线程(1 2 3 ...),但仍然没有找到解决问题的方法......我有一个这样的数据框:

prop1 prop2 prop3    prop4 
L30   3     bob      11.2
L30   54    bob      10
L30   11    john     10
L30   10    bob      10
K20   12    travis   10 
K20   1     travis   4 
K20   66    leo      10
Run Code Online (Sandbox Code Playgroud)

我想在 prop1 上做一个 groupby,同时,聚合所有其他列,但只使用唯一值。像那样:

prop1  prop2       prop3       prop4
L30    3,54,11,10  bob,john    11.2,10
K20    12,1,66     travis,leo  10,4
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的方法:

  1. df.groupby('prop1')['prop2','prop3','prop4'].apply(np.unique) 返回

AttributeError: 'numpy.ndarray' 对象没有属性 'index' PLUS TypeError: Series.name must be a hashable type

  1. 另外:.apply(lambda x: pd.unique(x.values.ravel()).tolist())它给出了一个列表作为输出,我想要列。

  2. df.groupby('prop1')['prop2','prop3','prop4'].unique() 本身不起作用,因为有多个列。

  3. .apply(f) 其中 f 是:

    def …

python unique dataframe pandas pandas-groupby

10
推荐指数
1
解决办法
7935
查看次数

标签 统计

dataframe ×1

pandas ×1

pandas-groupby ×1

python ×1

unique ×1