将Pandas代码循环两次并使Bot的运行时间加倍
我的样式代码循环了两次,使程序的运行时间增加了一倍。请帮我找出原因 我正在创建一个机器人,它可以从网站中获取特定字符串,并根据结果为包含该网站URL的数据框单元格着色。这是使用熊猫的样式设置功能完成的。输出和生成它的代码如下所示: style.applymap()方法使函数在数据帧中的指定单元格中循环,检查每个URL并为其单元格着色。然而,当我打印(条件_1)时,很明显它会检查每个网站两次,并将程序的运行时间增加一倍 有人能帮我弄清楚为什么会这样,以及如何让循环运行一次吗将Pandas代码循环两次并使Bot的运行时间加倍,pandas,loops,for-loop,dataframe,styling,Pandas,Loops,For Loop,Dataframe,Styling,我的样式代码循环了两次,使程序的运行时间增加了一倍。请帮我找出原因 我正在创建一个机器人,它可以从网站中获取特定字符串,并根据结果为包含该网站URL的数据框单元格着色。这是使用熊猫的样式设置功能完成的。输出和生成它的代码如下所示: style.applymap()方法使函数在数据帧中的指定单元格中循环,检查每个URL并为其单元格着色。然而,当我打印(条件_1)时,很明显它会检查每个网站两次,并将程序的运行时间增加一倍 有人能帮我弄清楚为什么会这样,以及如何让循环运行一次吗 #defining
#defining colour function. 'site' is variable that stores element in 'Article URL' cell.
def A_4thStreetWine_func(site):
site_download = rq.get(site)
#convert website to string and look for specified strings in it. returns Boolean.
condition_1 = '4th' in site_download.text \
and 'Street' in site_download.text \
and 'Project Runway'in site_download.text \
or '4th' in site_download.text \
and 'STREET' in site_download.text \
and 'Project Runway'in site_download.text
print(condition_1)
condition_2 = "403 Forbidden" in site_download.text
condition_3 = "Page not found" in site_download.text
if condition_1:
color = 'green'
elif condition_2:
color = 'orange'
elif condition_3:
color = 'purple'
else:
color = 'red'
return 'background-color: %s' % color
#colour 'Article URL' cells in rows 329 and 351 according to function. 'A_4thStreetWine_DF' is key of dataframe in dataframe dictionary (dfd).
dfd['A_4thStreetWine_DF'].loc[[329,351]].style.applymap(A_4thStreetWine_func, subset=['Article URL'])