Consider a Pandas data frame:
import pandas as pd
df = pd.DataFrame({
'a': pd.Series([1,1,1,2,3]),
'b': pd.Series(list('asdfg'))
})
I want to return all of the rows with duplicate values for column a
, including the first or last row. I can do this with
df[df['a'].duplicated() | df['a'].duplicated(take_last=True)]
Is there a better way?
You can
count
occurrences ofa
and returnvalues>1
for duplicated rows.