我试图比较两个数据帧,我希望有效地使用pandas (or numpy)
而不是一个带有if语句的嵌套for循环来解决一个特定的问题。下面是我的程序的一个小嗅探器
spindleload = {'time': ['2020-02-03 00:18:07', '2020-02-03 00:18:08', '2020-02-03 00:18:09', '2020-02-03 00:18:10', '2020-02-03 00:18:11', '2020-02-03 00:18:12', '2020-02-03 00:18:13', '2020-02-03 00:18:14', '2020-02-03 00:18:15', '2020-02-03 00:18:16', '2020-02-03 00:18:17'],
'value': [17,25,14,14,22,22,22,22,22,12,19]}
df1 = pd.DataFrame(data=spindleload)
cycletime = {'newtimestart': ['2020-02-03 00:18:08','2020-02-03 00:18:13'], 'newtimeend': ['2020-02-03 00:18:11', '2020-02-03 00:18:15']}
df2 = pd.DataFrame(data=cycletime)
现在,我希望循环它以获得df1中的time和value以及df2中newtimestart和newtimeend之间的相应值。下面是代码
output_graph_time = []
output_graph_value = []
for i in range(len(df2)):
for j in range(len(df1)):
if df1['time'][j] >= df2['newtimestart'][i]:
output_graph_time.append(df1['time'][j])
output_graph_value.append(df1['value'][j])
if df1['time'][j] == df2['newtimeend'][i]:
break
print(output_graph_time)
print(output_graph_value)
现在代码工作正常,但当它读取数百万行数据时无法执行,我也尝试实现Numba
,但内核无法处理并用于重新启动。
因此,我要求使用Numpy或Pandas矢量化方法来解决这个问题
此外,当我尝试应用矢量化方法时,我发现每次执行后都很难打破if条件
我知道
df1
是几百万行。如果您的df2
不是太大,您可以执行交叉合并:输出:
您可以在daterange中转换df2并分解,然后使用
isin
过滤器df1从字符串转换为日期时间(忽略此步骤,因为值已经是日期时间)
映射为
pd.date_range
和explode
,然后使用isin
进行筛选这与您的输出类似:
相关问题 更多 >
编程相关推荐