我进入了无数线程(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)
我尝试了不同的方法:
df.groupby('prop1')['prop2','prop3','prop4'].apply(np.unique)
返回 AttributeError: 'numpy.ndarray' 对象没有属性 'index' PLUS TypeError: Series.name must be a hashable type
另外:.apply(lambda x: pd.unique(x.values.ravel()).tolist())它给出了一个列表作为输出,我想要列。
df.groupby('prop1')['prop2','prop3','prop4'].unique() 本身不起作用,因为有多个列。
.apply(f) 其中 f 是:
def …