【发布时间】:2022-01-01 08:24:43
【问题描述】:
今天我一直致力于合并和编辑数据框,但我一直被一个非常具体的部分所困扰。我有一列包含某些水果的名称,然后是这样的人的名字:
| Fruit | Person |
|---|---|
| Banana | Jake |
| Banana | Paul |
| Carrot | Nancy |
| Carrot | Sydney |
| Carrot | Jane |
请注意,“Person”列始终是唯一的。我的目标是在第三列或第四列中获得类似的内容:
| Fruit | Person1 | Person2 | Person3 |
|---|---|---|---|
| Banana | Jake | Paul | |
| Carrot | Nancy | Sydney | Jane |
我试过这个:
first = df.drop_duplicates(subset=['Zone','District','Area'],keep='First',inplace = True)
second = df.drop_duplicates(subset=['Zone','District','Area'],keep='Last',inplace = True)
稍后将它们合并在一起,但这不会得到中间的那些,例如我的示例中的“Syndey”。我找到了这个: HERE
但我不太了解groupby 部分。我希望这会有所帮助,并感谢您的时间和耐心。
【问题讨论】:
-
感谢@MayankPorwal 的编辑,我对此还是很陌生!
-
解决方案在 Q/A no.10 中被欺骗
标签: python pandas dataframe duplicates