简体   繁体   English

删除 Pandas 中的重复项,但保存其他列 PANDAS 中的值

[英]Remove duplicated in pandas but save values from other column PANDAS

0 id   description
1 11   pandas
2      is very
3       good

The id is in the 3 lines like: id 在 3 行中,例如: 在此处输入图片说明

I want to remove id duplicated but conserve all the description.我想删除重复的 id 但保留所有描述。 I use:我用:

pd.drop_duplicates(subset="id", keep="first") 

And that returns me:这让我返回:

0 id description
1 11 pandas

But i want但我想要

0 id description
1 11 pandas is very good.

You can do use groupby + agg :您可以使用groupby + agg

df = df.groupby('id').agg(' '.join).reset_index()
print(df)

   id             description
0  11    pandas is very  good

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM