我有一个看起来像这样的DataFrame:
Product Family Date Price Type Price
Cereal 2017-11 1 4.99
Cereal 2017-12 1 4.59
Cheese 2017-11 1 3.99
Cheese 2017-12 2 3.99
Oats 2017-11 3 5.99
Oats 2018-12 3 5.99数据按月分组。我想要的是为每个产品系列输出/标记价格类型与上个月不同的行。所以在这个例子中,我想要返回类似这样的东西:
Product Family Date Price Type Price Flag
Cheese 2017-11 1 3.99 price type change
Cheese 2017-12 2 3.99 price type change我试图制定布尔语句并将其分配到“Flag”列,但我不断收到错误,并且每个产品系列的数据不是每月进行比较。
发布于 2018-01-10 00:25:41
df['Flag']=df.groupby('ProductFamily').PriceType.transform('nunique').gt(1).map({True:'Flag',False:'Not Flag'})
df
Out[59]:
ProductFamily Date PriceType Price Flag
0 Cereal 2017-11 1 4.99 Not Flag
1 Cereal 2017-12 1 4.59 Not Flag
2 Cheese 2017-11 1 3.99 Flag
3 Cheese 2017-12 2 3.99 Flag
4 Oats 2017-11 3 5.99 Not Flag
5 Oats 2018-12 3 5.99 Not Flaghttps://stackoverflow.com/questions/48172438
复制相似问题