首页 > 解决方案 > Filter dataframe by first order per customer

问题描述

I would like some help to solve the following problem using Pandas in Python.

I have a dataframe about the customers' transactions - in random order, which contains the following columns, along with datatypes:

I need to find a dataframe that contains all the first (chronological) transactions for every customer. The final dataframe should contain the same columns as the original one.

So far I have tried to use some "group by", together with aggregate functions, but I cannot seem to get the first transaction in chronological order, instead of the first in order of appeareance.

Any thoughts?

标签: pythonpandasdatetimepandas-groupby

解决方案


这将为您提供每个客户的最早观察结果:

df_first = df.sort_values('transaction_date').groupby('user_id').head(1)

推荐阅读