How to find duplicate names using pandas?

前端 未结 6 1281
無奈伤痛
無奈伤痛 2020-12-14 02:27

I have a pandas.DataFrame with a column called name containing strings. I would like to get a list of the names which occur more than once in the c

6条回答
  •  生来不讨喜
    2020-12-14 02:49

    Most of the responses given demonstrate how to remove the duplicates, not find them.

    The following will select each row in the data frame with a duplicate 'name' field. Note that this will find each instance, not just duplicates after the first occurrence. The keep argument accepts additional values that can exclude either the first or last occurrence.

    df[df.duplicated(['name'], keep=False)]
    

    The pandas reference for duplicated() can be found here.

提交回复
热议问题