![](/img/trans.png)
[英]How to drop duplicates for the following data-frame based on multiple conditions?
[英]How to drop duplicates based on two or more subsets criteria in Pandas data-frame
可以說這是我的數據框
df = pd.DataFrame({ 'bio' : ['1', '1', '1', '4'],
'center' : ['one', 'one', 'two', 'three'],
'outcome' : ['f','t','f','f'] })
看起來像這樣...
bio center outcome
0 1 one f
1 1 one t
2 1 two f
3 4 three f
我想刪除第 1 行,因為它與第 0 行具有相同的生物和中心。我想保留第 2 行,因為它與第 0 行具有相同的生物但不同的中心。
像這樣的東西不會基於 drop_duplicates 輸入結構工作,但這是我想要做的
df.drop_duplicates(subset = 'bio' & subset = 'center' )
有什么建議么?
編輯:改變 df 以適應正確答案的例子
你的語法是錯誤的。 這是正確的方法:
df.drop_duplicates(subset=['bio', 'center', 'outcome'])
或者在這種特定情況下,只需簡單地:
df.drop_duplicates()
兩者都返回以下內容:
bio center outcome
0 1 one f
2 1 two f
3 4 three f
查看df.drop_duplicates
文檔了解語法細節。 subset
應該是一系列列標簽。
上一個答案非常有幫助。 它幫助了我。 我還需要在代碼中添加一些東西來獲得我想要的東西。 所以,我想在這里補充一下。
數據框:
bio center outcome
0 1 one f
1 1 one t
2 1 two f
3 4 three f
實施drop_duplicates
后:
bio center outcome
0 1 one f
2 1 two f
3 4 three f
注意索引。 他們搞砸了。 如果有人想從0, 2, 3
支持正常索引,即0, 1, 2
:
df.drop_duplicates(subset=['bio', 'center', 'outcome'], ignore_index=True)
Output:
bio center outcome
0 1 one f
1 1 two f
2 4 three f
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.