Calculate total number of unique rows and save the value in a new column

Viewed 50

Sample Data

How do I get from that to this:

enter image description here

I know I can count using len() by putting condition inside but actual data is really big, it has around 14k rows. So it is not realistic to find each unique rows and then count the total number of each unique rows. Is there any easier solution ?

2 Answers

you can use groupby on all columns and count (pick one column):

df.groupby(df.columns)['Date'].count()

Use groupby, size and reset_index:

df.groupby(list(df.columns)).size().reset_index(name = "total")
Related