Home > other >  Python saving customer minimum order date value per customer from multiple values
Python saving customer minimum order date value per customer from multiple values

Time:12-27

I have df :

df = pd.DataFrame({"customer": ["foo", "foo", "foo", "foo", "foo", "bar", "bar", "bar", "bar"],
                   "order_date": ["12-12-2022", "12-12-2020", "12-12-2019", "12-12-2018", "12-12-2022", "12-12-2022", "12-12-2015", "12-12-2019", "12-12-2017"]})

from which I need the 1st order date (min value) per customer, saved as a new df. i.e.

   customer   1st_order_date
0  foo  12-12-2018
1  bar  12-12-2015

how to do it in python?

CodePudding user response:

Group records by customer and aggregate a minimal order_date for each group:

In [71]: df.groupby('customer')['order_date'].agg('min').to_frame(name='1st_order_date').reset_index()
Out[71]: 
  customer 1st_order_date
0      bar     12-12-2015
1      foo     12-12-2018
  • Related