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

标签 python pandas timestamp

在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

文档中描述了与此类似的内容:link

关于python - 如何在 python 中对多个数据帧使用单个过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61414450/

相关文章:

python - 使用 urllib2 进行 POST 的 400 错误请求

python - 如果列名以数字开头,则删除 pandas 列

timestamp - PostgreSQL如何设置带时间戳的主键列自动填充

javascript - 如何将日期转换为时间戳字符串 Angular 2

Python - 找到最近的时间戳

python - 重力问题

Python读取设备管理器信息

python - 如何量化 cartToPolar 输出以估计连续帧之间的流量 Python OpenCV?

python - Pandas:具有更新的、更长的列的数据框

python - 更改 Pandas DataFrame 中的特定值(其中有混合类型)