Identifying customer first buy on a dataframe

Question:

I have a huge database with millions of rows containing transactions information :

Client_ID, Date , Product, Price
Jhon, 2022-08-25, google, USD 10,00
Jhon, 2022-09-26, uber, USD 25,00

I am trying to create a new dataframe where I can identify monthly, which clients are "new" and which are not, so I wonder to create a new column on my DF containing the date of first buy.

Client_ID, Date , Product, Price , Cliente_firt_buy
Jhon, 2022-08-25, google, USD 10,00, 2022-08-25
Jhon, 2022-09-26, uber, USD 25,00, 2022-08-25

Something like this, so I could summary my date segregating new and old consumers.

I am struggling how do apply this to the DF.

Thanks for any advice,

Asked By: FábioRB

||

Answers:

Try this

df.sort_values(['Client_ID','Date'],ascending=True).groupby('Client_ID').first()
Answered By: Alex
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.