创建条件以替换pandas中缺失的值,包括string、Date和Time



在我的df中,我试图根据日期和时间以及当天发生的挤奶事件替换列'Amount In (L)'中的值。

Milk Event          Date        Time     Amount in (L)
Start    04/08/2001       09:00               nan 
End    04/08/2001       10:00              1500   
Start    04/08/2001       14:00               nan 
End    04/08/2001       15:00              2000   
Start    05/08/2001       08:00               nan 

我不确定是否使用for循环或if语句来创建用以前的'End'事件量替换nan的条件。我希望它看起来像:

Milk Event          Date        Time     Amount in (L)
Start    04/08/2001       09:00                 0 
End    04/08/2001       10:00              1500   
Start    04/08/2001       14:00              1500 
End    04/08/2001       15:00              2000   
Start    05/08/2001       08:00              2000

如果可能的话,可以使用:

df['Amount in (L)'] = pd.to_numeric(df['Amount in (L)'], errors='coerce').ffill().fillna(0)

最新更新