0

I bring my solution in order to optimize several loops having if conditions

Attached, initial code world with loops and conditions, which took me about 5/6 seconds per iteration

matches = pd.read_csv('archive/matches.csv')
participants = pd.read_csv('archive/participants.csv')
statsAll = ['archive/stats1.csv','archive/stats2.csv']
stats = pd.concat((pd.read_csv(f) for f in statsAll), ignore_index=True)

for value in tqdm(merge):
    for index, match in matches.iterrows():
        if value["gameid"] == match["gameid"]:
            for index, participant in participants.iterrows():
                if match["id"] == participant["matchid"]:
                    for index, stat in stats.iterrows():
                        if participant["id"] == stat["id"]:
                            if stat["win"] == 0:
                                value["victory"] =1
                            else:
                                value["victory"] =0
                            break
                    else:
                        continue
                    break
            else:
                continue
            break

And here is the new code that only works with a for loop and merges! Using panda is needed for my example

mergeTwo = pd.read_json('merge/mergeUpdate.json')
matches = pd.read_csv('archive/matches.csv')
participants = pd.read_csv('archive/participants.csv')
statsAll = ['archive/stats1.csv','archive/stats2.csv']
stats = pd.concat((pd.read_csv(f) for f in statsAll), ignore_index=True)

for index, value in tqdm(mergeTwo.iterrows()):
    match = value.to_frame().T.merge(matches, on='gameid')
    participant = match.merge(participants, left_on='id', right_on='matchid')
    stat = participant.iloc[0].to_frame().T.merge(stats,left_on='id_y',right_on='id')
    
    value["win"] = int(stat.iloc[0]["win"])

hoping that some will find their happiness there !

Akael
  • 61
  • 6
  • Looks like a merge, read the duplicate carefully, try to apply it to your data and give more details if you fail (provide the merging code you tried!) – mozway Oct 04 '22 at 07:37

0 Answers0