如何在python中对多个数据帧使用单个过滤器



在Python中,我希望对多个数据帧使用相同的筛选条件。

我目前拥有的是:

filtered_df1=df1[(df1['Timestamp'] > Lower_limit) & (df1['Timestamp'] < Upper_limit)]
filtered_df2=df2[(df2['Timestamp'] > Lower_limit) & (df2['Timestamp'] < Upper_limit)]
filtered_df3=df3[(df3['Timestamp'] > Lower_limit) & (df3['Timestamp'] < Upper_limit)]

为了使代码更有效率,我尝试了以下操作,但没有成功:

Lower_limit, Upper_limit= '2019-12-4 06:00:00', '2019-12-6'
dfs = [df1, df2, df3]
for df in dfs:
df=df[(df['Timestamp'] > Lower_limit) & (df['Timestamp'] < Upper_limit)]

示例数据帧的链接如下:https://www.dropbox.com/sh/75uocnegx831d0x/AAAcp_Su-Z4ImGY-OUZsumusa?dl=0

如有任何关于如何改进此代码的建议,我们将不胜感激。谢谢

也许这将有助于

#convert to datetime
Lower_limit, Upper_limit= '2019-12-4 06:00:00', '2019-12-6'
Upper_limit = pd.to_datetime(Upper_limit)
Lower_limit = pd.to_datetime(Lower_limit)
#read in columns and change Timestamp to another name
#Timestamp is a type ... cant compare time with type
df1 = pd.read_csv('df1.csv',sep=';', parse_dates = ['Timestamp']).rename(columns={"Timestamp":'timer'})
df2 = pd.read_csv('df2.csv',sep=';', parse_dates = ['Timestamp']).rename(columns={"Timestamp":'timer'})
#run function
expression = '@Lower_limit < timer < @Upper_limit'
func = lambda x: x.query(expression)
filter1, filter2 = [func(df) for df in [df1,df2]]
#check any one
filter1.head()
timer                 Value
13  2019-12-04 06:10:00 27.282425
14  2019-12-04 06:20:00 27.266042
15  2019-12-04 06:30:00 27.212147
16  2019-12-04 06:40:00 27.272867
17  2019-12-04 06:50:00 27.242088

文档中描述了类似的内容:链接

最新更新