python - 你将如何优化这个简短但非常慢的 Python 循环?
问题描述
我正在从 R 切换到 Python。不幸的是,我发现虽然某些结构在 R 中几乎可以立即运行,但在 Python 中却需要几秒钟(甚至几分钟)。阅读后,我发现在 pandas 中强烈建议不要使用 for 循环,建议使用其他替代方法,例如矢量化和应用。
在此示例代码中:从从最小值到最大值排序的一列值中,保留在长度为“200”的间隙之后出现的所有值。
import numpy as np
import pandas as pd
#Let's create the sample data. It consists of a column with random sorted values, and an extra True/False column, where we will flag the values we want
series = np.random.uniform(1,1000000,100000)
test = [True]*100000
data = pd.DataFrame({'series' : series, 'test':test })
data.sort_values(by=['series'], inplace=True)
#Loop to get rid of the next values that fall within the '200' threshold after the first next valid value
for i in data['series']:
if data.loc[data['series'] == i,'test'].item() == True:
data.loc[(data['series'] > i) & (data['series'] <= i+200 ) ,'test' ] = False
#Finally, let's keep the first values after any'200' threshold
data = data.loc[data['test']==True , 'series']
是否可以将其转换为函数、矢量化、应用或除“for”循环之外的任何其他结构以使其几乎立即运行?
解决方案
这是我的while
循环方法:
head = 0
indexes = []
while head < len(data):
thresh = data['series'].iloc[head] + 200
indexes.append(head)
head += 1
while head < len(data) and data['series'].iloc[head] < thresh:
head+=1
# output:
data = data.iloc[indexes]
# double check with your approach
set(data.loc[data['test']].index) == set(data.iloc[indexes].index)
# output: True
以上花费了 984 毫秒,而您的方法花费了 56 秒。
推荐阅读
- reactjs - 为什么循环遍历数据和绘制多边形不起作用
- html - 添加窗帘后按钮展开
- python - matplotlib 中图例的标记比例问题
- c# - LINQ 文件数组拆分
- session - 加载会话需要外部网站,而不是通过 iFrame
- c# - Omu.ValueInjecter 不向后映射
- sql-server - 在 Azure 数据仓库中创建触发器时,“CREATE”附近的语法不正确
- javascript - Agora 网页屏幕共享我们如何停止共享
- python - 编写一个程序,在没有 itertools 的情况下按升序打印出整数 1,...,m 的所有不同的 n 元组
- google-chrome - chrome下载api在tiktok上不起作用