如何在条件满足之前用N行中的某些行来子集条件行,比我的代码更快?

2024-04-25 19:20:53 发布

您现在位置:Python中文网/ 问答频道 /正文

因为我的数据集是时间序列,我有30个不同的数据帧,每个数据帧有超过10000行。我想检查,温度值低于40之前的趋势。你知道吗

所以,我想在温度值低于40时对行进行子集划分,在温度值低于40之前对24行进行子集划分。你知道吗

我已经尝试了一些代码,唯一有效的代码是下面。但是子集需要更长的时间(比如一个数据帧需要10分钟以上)。所以,我的代码不好。所以我想知道用python编写的代码,它可以更快地生成子集。你们能帮帮我吗?你知道吗

df=temperature_df.copy()
drop_temperature_df=pd.DataFrame()

# get the index during drop temperature
drop_temperature_index=np.array(df[df[temperature]<40].index)

# subset the data frame for 24 hours before drop temperature
for i,index in enumerate(drop_temperature_index):
    drop_temperature_df=drop_temperature_df.append(df.loc[index-24:index,:])

K['K_{}'.format(string)]=drop_temperature_df.copy() #save the subset data frame

就像下面的数据一样, 我在2018年1月26日8点的温度低于40度 所以,我想把40以下的点子集,前面有24行(1/25/2018 0800到1/26/2018 0800)。你知道吗

enter image description here


Tags: the数据代码dffordataindex时间