熊猫选择出现n次的重复

时间:2016-09-10 19:11:23

标签: python pandas dataframe duplicates

我有一个数据框如何选择仅出现两次的重复

import pandas as pd

df=pd.DataFrame({'Name':['Two','Twice','Twice','three','three','three','one', 'Two'],
'key':[2,2,2,1,1,3,1,1,],
'Last':['Foo','Macy','Gayson','Simpson','Diablo','Niggah','Simpson', 'Mortimer']
})


r=df[df.duplicated(subset=['Name'], keep =False)]


print(r)

所以我会得到:

       Last   Name  key
0       Foo    Two    2
1      Macy  Twice    2
2    Gayson  Twice    2
7  Mortimer    Two    1

1 个答案:

答案 0 :(得分:0)

试试这个:

In [80]: df.groupby('Name').filter(lambda x: len(x) == 2)
Out[80]:
       Last   Name  key
0       Foo    Two    2
1      Macy  Twice    2
2    Gayson  Twice    2
7  Mortimer    Two    1