我有一个包含相关信息的数据帧,我想按一列(比如id)分组,其他具有相同id的列用“|”连接。但是,当我运行代码时,我的大多数列都会丢失(只有前3列出现),我不知道出了什么问题。你知道吗
我的代码是:
df = df.groupby('id').agg(lambda col: '|'.join(set(col))).reset_index()
例如,我的数据
id words ... (other columns here)
0 a asd
1 a rtr
2 b s
3 c rrtttt
4 c dsfd
我想要
id ... (other columns here)
a asd|rtr
b s
c rrtttt|dsfd
但我的其他专栏也是这样分组的。现在,我的其他列只是没有出现在我的输出数据集中。不确定出了什么问题。谢谢!你知道吗
转换为字符串之前,您可以使用
agg(set)
和applymap
避免lambda:最小可验证示例
相关问题 更多 >
编程相关推荐