Pandas获取一列值作为另一列下一个单元格的条件

0 投票
1 回答
42 浏览
提问于 2025-04-13 15:35

我在pandas里有一个表格,这个表格记录了时间和产品的价格。

为了分析,我想添加两列,分别显示下一个价格变化超过$100和低于$100的时间。

比如,如果我在09:19这个单元格,接下来价格超过$100的时间是14:02,而低于$100的时间是11:39,那么14:02和11:39就应该分别填在09:19这一行的对应列里。

同样的,对于09:56这个单元格,接下来价格超过$100的时间是14:02,而低于$100的时间是12:18,这两个值也应该填在09:56这一行。

Time    Price_of_product    Time when next price moving by 100 up   Time when next price moving by 100 Down
09:19:00    3252.25     
09:24:00    3259.9      
09:56:00    3199.4      
10:17:00    3222.5      
10:43:00    3191.25     
11:39:00    3143        
12:18:00    2991.7      
13:20:00    3196.35     
13:26:00    3176.1      
13:34:00    3198.85     
13:37:00    3260.75     
14:00:00    3160.85     
14:02:00    3450        
14:19:00    3060.5      
14:30:00    2968.7      
14:31:00    2895.8      
14:52:00    2880.7      
14:53:00    2901.55     
14:55:00    2885.55     
14:57:00    2839.05     
14:58:00    2871.5      
15:00:00    2718.95     

这个任务我需要在很多数据集上重复进行,所以我想用pandas/python来帮忙。

谢谢大家!

1 个回答

1

这样做就可以解决问题了:

df['Time_when_next_price_moving_by_100_up'] = np.nan
df['Time_when_next_price_moving_by_100_down'] = np.nan

for i, row in df.iterrows():
    time_up = np.nan
    time_down = np.nan

    for j in range(i+1, len(df)):
        diff = df.iloc[j]['Price_of_product'] - row['Price_of_product']
        if diff > 100:
            time_up = df.iloc[j]['Time']
        elif diff < -100:
            time_down = df.iloc[j]['Time']
        
        if not pd.isna(time_up) or not pd.isna(time_down):
            break
    
    df.at[i, 'Time_when_next_price_moving_by_100_up'] = time_up
    df.at[i, 'Time_when_next_price_moving_by_100_down'] = time_down

撰写回答