最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

lambda - Python Pandas - Prior Price Change Date based on Multiple Columns - Stack Overflow

programmeradmin1浏览0评论

I've done my due diligence and still can't find what I need to make this happen.

This is a sample dataset of what I'm using. I need to add the column "date_price_change" for that particular Store/SKU combination where the "price" changed last.

I've tried-

df['date_price_change'] = df.groupby['store_nbr', 'product_sku']['date'].max()

but that's giving me an error, plus I need to make sure that the "date change" is less than the row's date.

TIA

I've done my due diligence and still can't find what I need to make this happen.

This is a sample dataset of what I'm using. I need to add the column "date_price_change" for that particular Store/SKU combination where the "price" changed last.

I've tried-

df['date_price_change'] = df.groupby['store_nbr', 'product_sku']['date'].max()

but that's giving me an error, plus I need to make sure that the "date change" is less than the row's date.

TIA

Share Improve this question asked Mar 4 at 20:31 DerwoodyDerwoody 325 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1
df = df.sort_values(by=['store_nbr', 'product_sku', 'date'])
df['price_change'] = df.groupby(['store_nbr', 'product_sku'])['retail'].apply(lambda x: x != x.shift()).values
df['date_price_change'] = df['date'].where(df['price_change']).ffill()
发布评论

评论列表(0)

  1. 暂无评论