【问题标题】:Re-write a for loop in python在python中重新编写一个for循环
【发布时间】:2022-06-15 13:17:12
【问题描述】:

我有这段代码,但由于某种原因,它一直在运行,无法完成。有没有办法解决这个问题或重写它?

l = []
w = []
players_w = pd.unique(df2["winner_name"])
players_l = pd.unique(df2["loser_name"])
for i,row in df1.iterrows():
    winner = row["Winner"]
    loser = row["Loser"]
        
    w1 = process.extract(winner, players_w)
    l1 = process.extract(loser, players_l)
        
    if len(w1) > 0:
        w.append(w1[0][0])
    else:
        w.append("")
            
    if len(l1) > 0:
        l.append(l1[0][0])
    else:
        l.append("")
            
df1["winner"] = w
df1["loser"] = l
    
d[year] = [df1, df2]

【问题讨论】:

标签: python pandas database for-loop statistics


【解决方案1】:

看看我对这个问题的回答:
python nested for loop keeps running indefinitely, yet the intended list is being created

如果您等待,您的代码将会完成,只是速度很慢。您应该能够简单地使用波纹管来获得更快/更清洁的解决方案:

df1["loser"] = lprocess.extract(loser, players_l)
df1["winner"] = process.extract(winner, players_w)

您可能还需要使用.fillna("") 将Nans 替换为空字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-01
    • 2019-06-21
    • 2021-02-17
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    相关资源
    最近更新 更多