Finding duplicate rows in a Pandas Dataframe then Adding a column in the Dataframe that states if the row is a duplicate

Viewed 4132

I have a pandas dataframe that contains a column with possible duplicates. I would like to create a column that will produce a 1 if the row is duplicate and 0 if it is not.

So if I have:

     A|B
1    1|x
2    2|y
3    1|x
4    3|z

I would get:

     A|B|C
1    1|x|1
2    2|y|0
3    1|x|1
4    3|z|0

I tried df['C'] = np.where(df['A']==df['A'], '1', '0') but this just created a column of all 1's in C.

1 Answers
Related