【问题标题】:Is there a way in Pandas to fill down the previous value with condition?Pandas 有没有办法用条件填充先前的值?
【发布时间】:2022-01-12 17:58:10
【问题描述】:

我有一个如下表,想根据条件填写同类别的Stage if Stage = "Delivered" then fill down "Delivered" to all the next rows else if Stage = "Paid" then fill down "Paid" to all the next rows

Category Date Stage
A 2021-11-01 Ordered
A 2021-12-01 Paid
A 2022-01-01
B 2021-08-01
B 2021-09-01 Ordered
B 2021-10-01 Paid
B 2021-11-01 Ordered
B 2021-12-01 Delivered

结果应该是这样的:

Category Date Stage
A 2021-11-01 Ordered
A 2021-12-01 Paid
A 2022-01-01 Paid
B 2021-08-01
B 2021-09-01 Ordered
B 2021-10-01 Paid
B 2021-11-01 Paid
B 2021-12-01 Delivered

有人可以帮忙吗?我真的很感激!

【问题讨论】:

  • 什么是条件?
  • 如果条件意味着每组然后需要dupe
  • @jezrael 也许你有一个很好的解决方案?

标签: python pandas dataframe


【解决方案1】:

您可以使用maskcombine_first

假设您的数据框已经按Date 列排序。

df['Stage'] = df['Stage'].mask(~df['Stage'].isin(['Paid', 'Delivered'])) \
                         .groupby(df['Category']).ffill() \
                         .combine_first(df['Stage'])
print(df)

# Output
  Category        Date      Stage
0        A  2021-11-01    Ordered
1        A  2021-12-01       Paid
2        A  2022-01-01       Paid
3        B  2021-08-01           
4        B  2021-09-01    Ordered
5        B  2021-10-01       Paid
6        B  2021-11-01       Paid
7        B  2021-12-01  Delivered

【讨论】:

  • 这对我的数据非常有效。非常感谢。
  • @anewone 如果答案有用,也请点赞。
猜你喜欢
  • 1970-01-01
  • 2022-06-24
  • 1970-01-01
  • 2019-06-07
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
相关资源
最近更新 更多