【发布时间】:2018-11-15 16:53:52
【问题描述】:
我将我的数据分组如下: df1 = df.groupby(['Store', 'Region', 'Item'])
STORE REGION
Store1 West Item price result
1 1.00 0
2 2.00 0
3 2.00 0
2.50 1
4 1.00 0
1.00 0
.50 1
1.00 1
基本上我想用 1 和 0 表示价格是否发生变化,如果没有价格变化。如果每件商品只有一个价格,那么我希望在这些情况下也返回 0。
我已经用下面的代码试过了,但是失败了:
data_cleaned.groupby(['STORE', 'REGION', 'Item'])['price'].apply(lambda x: 1 if x == x.change.shift(1) else 0})
【问题讨论】:
标签: python pandas pandas-groupby