Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merge duplicate rows by adding a column 'count' [duplicate]

Tags:

python

pandas

I want to merge duplicate rows by adding a new column 'count'

Initial Data frame

Final dataframe that I want

Final Dataframe that I want

rows can be in any order

like image 827
Rahul Chaudhary Avatar asked Mar 27 '26 17:03

Rahul Chaudhary


1 Answers

You can use:

df["count"] = 1
df = df.groupby(["user_id", "item_id", "total"])["count"].count().reset_index()
like image 148
pissall Avatar answered Mar 30 '26 11:03

pissall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!