python - 如何对涉及两个数据帧的条件的熊猫行迭代进行矢量化?
问题描述
我有两个数据框df1
和df2
. 我需要迭代df1
行以获取时间戳,并根据此时间戳+- 1000 milliseconds
过滤来自df2
. 下面的代码片段清楚地解释了它,
dataframes = []
for i in df1.index:
tempdf = df1[df1.index.values == i]
attributeName = tempdf['EventId'].iloc[0]
timeStamp = tempdf['Timestamp'].iloc[0]
fehlerStatus = tempdf['FehlerStatus'].iloc[0]
tempdf2 = df2[(df2['DateTime']>=timeStamp + datetime.timedelta(milliseconds=0)) &
(df2['DateTime']<=timeStamp + datetime.timedelta(milliseconds=1000))].sort_values(by='DateTime', ascending=True).reset_index(drop=True)
if not tempdf2.empty:
tempdf2['TargetAttributePlusStatus'] = attributeName.replace(' ', '_') + fehlerStatus.replace(' ', '_')
dataframes.append(tempdf2)
df = pd.concat(dataframes, axis=0)
上面的代码需要永远执行。有没有更方便的方法来矢量化它?
解决方案
推荐阅读
- reactjs - 在 useState 钩子中设置导致为空
- javascript - 如何通过脚本将数据传递到网页?
- javascript - javascript:如何替换选择:单击后的图标?
- mysql - 引导模型,使用节点 js 从模态到 mySQL 数据库的信息
- node.js - 如何在猫鼬中从.exec返回项目,exec中的console.log返回元素但在外部返回未定义
- python - 如何合并多个工作表并用工作表名称重命名列名?
- c++ - 我的 buildHeap 函数中的逻辑错误是什么?
- sql - 有没有办法防止用户在 PosgreSQL 中进行洪水插入?有某种速率限制吗?
- reactjs - 如何使用下一个优化图像将外部图像导入静态文件夹?
- python-3.x - 第二个程序运行时 Python 脚本不启动