我有一个包含custid、transaction date列等的sales表。我在custid列上使用groupby,然后使用agg方法来获取max date(获取特定客户的最新交易日期)和min date(获取他在商店的第一个交易日期)。在
我的代码如下:
sales['transdate'] = pd.to_datetime(sales['transdate']) # Converting the transdate column from string to timestamps.
sales['custid'].groupby.transdate({'count': np.count_nonzero ,'first': np.min, 'last' : np.max})
我想知道是否可以
calculate min and max between dates by using np.min/max methods. or should I be using some other datetime related methods?
您应该使用
groupby.agg
来应用多个聚合函数。在还请注意,对于Pandas,可以通过字符串调用许多聚合函数。在这种情况下,可以使用}。建议使用字符串,因为Pandas将字符串表示映射到经过测试的高效算法。在
'size'
、'min'
和{下面是一个演示:
相关问题 更多 >
编程相关推荐