【发布时间】:2019-05-15 10:23:59
【问题描述】:
在我的 Dataframe 中,我有一个字段显示超时订购的产品的状态。这可以是“新”、“已取消”、“已填充”或“部分”。我总结了记录的每个 Order(Orderid) 的模式,并对可能出现的不同模式进行了计数。然而,这导致了超过 1385 种不同的模式。我现在想将这些模式压缩到 bin 中,例如,如果订单状态为:New、New、Cancelled、New、Filled,则将压缩为:New、Cancelled、New、Filled。
这将与以下模式放在同一个 bin 中:New、New、New、Cancelled、Cancelled、New、New、Filled。
这是原始数据的样子:
按每个 OrderID 分组一次:
为了查看数据中存在的 OrderStatus 模式,应用了以下代码:
def status_transition_with_timestamp(each_grouped_df):
sorted_df = each_grouped_df.sort_values('timestamp', ascending=True)
concatenated_transition = ','.join(sorted_df['ostatus'])
return concatenated_transition
result = df_grouped['ostatus'].agg(status_transition_with_timestamp)
result.groupby('ostatus').count()
【问题讨论】: