%%time
match_ids_dict = {}
for index ,row in df_match.iterrows():
team1 = row\['team1'\] + ' Vs ' + row\['team2'\]
team2 = row\['team2'\] + ' Vs ' + row\['team1'\]
match_ids_dict\[team1\] = row\['match_id'\]
match_ids_dict\[team2\] = row\['match_id'\]
match_ids_dict
I want more efficient method of this problem?
I tried creating example data, hopefully its the situation you're in. You can use dict
and zip
to make a dictionary from your columns without having to do the slow loop. The update
line is adding the team2
- team1
data to the same dict as the team1
- team2
info
df_match = pd.DataFrame({
'team1':['A','B','C','D'],
'team2':['E','F','G','H'],
'match_id':[1,2,3,4],
})
match_ids_dict = dict(zip(df_match['team1']+' Vs '+df_match['team2'], df_match['match_id']))
match_ids_dict.update(dict(zip(df_match['team2']+' Vs '+df_match['team1'], df_match['match_id'])))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.