import pandas as pd
sim = [['Matthew Stafford', 15, 13, 12], ['Dalvin Cook', 18, 16, 17], ['Daniel Jones', 17, 17, 15], ['Joe Mixon', 16, 15, 15]]
col = ['Player', 1 , 2, 3]
NFL_Sim = pd.DataFrame(sim, columns=col)
list = [['Matthew Stafford', 'Dalvin Cook'], ['Daniel Jones', 'Joe Mixon']]
col = ['QB', 'RB']
output_lines = pd.DataFrame(list, columns=col)
for x in range(1, 4):
output_lines[x] = output_lines.QB.map(NFL_Sim.set_index('Player')[x].to_dict()) + output_lines.RB.map(NFL_Sim.set_index('Player')[x].to_dict())
print(output_lines)
QB RB 1 2 3
0 Matthew Stafford Dalvin Cook 33 29 29
1 Daniel Jones Joe Mixon 33 32 30
The desired output is correct, however as I scale this up, I have thousands of columns in the NFL_Sim dataframe which makes mapping extremely slow. Is there a more efficient way to write this for loop? Or convert output_lines to a list first? I'm really not sure what's best.