weixin_41333919 2021-11-14 23:15 采纳率: 66.7%
浏览 146
已结题

python中提升for嵌套循环的速度

两个数据帧 df1和shops,循环两个数据帧,如果df1的列12的值与shops的列0的值相等,则df1的13的值填入shops的列1的值

for j in range(len(df1)):
   for i in range(len(shops)):
       if df1[12][j] == shops[0][i]:
            df1[13][j] = shops[1][i]
       else
           continue

这种循环速度最慢,我又想使用iterrows,代码如下

for index,row in df1.iterrows():
    for index1,row1 in shops.iterrows():
       if df[12][index]== shops[0][index1]:
            df1[13] [index]= shops[1][index1]
       else
           continue

以上两种方式速度依旧不够快,很慢,没达到我想的速度
请问有更好的更快的写法?

  • 写回答

2条回答 默认 最新

  • 波༒风 2021-11-15 10:42
    关注

    麻烦问下,两个数据,长度一样么?

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月4日
  • 修改了问题 11月15日
  • 修改了问题 11月14日
  • 创建了问题 11月14日