python - 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:
- user_id object;
- transaction_date datetime64[ns];
- account_creation_date datetime64[ns];
- transaction_id object;
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?
解决方案
这将为您提供每个客户的最早观察结果:
df_first = df.sort_values('transaction_date').groupby('user_id').head(1)
推荐阅读
- gradle - 为什么直接调用 groovy 闭包不起作用,但使用 .call() 可以?
- java - 如何模拟包含可以抛出 NPE 的变量的类
- python-3.x - 我如何知道是否已经在解析器上调用了 add_subparsers()
- node.js - 汇总和分配随机获胜者的更好方法
- javascript - AngularJS:如何创建一个自定义过滤器,可以将字符串的数字转换为 h:m 格式?
- ios - 使用 systemBlueColor 作为按钮背景
- jquery - 从 _form.html.erb 到 index.html.erb 的 Ajax 发布
- google-apps-script - 如何重新执行 App Maker Datasource 的查询脚本
- gdb - 如何转换为模板类型?
- sas - 回顾 SAS