I have attached an image of my dataframe, and the code of the methods I tried. My goal is to switch the first half of the values in a row with the second half of the values in that row if the row satisfies a condition.
The first method checks if the condition is true (values need to be switched), and then assigns the new values directly to the original dataframe.
The second methods checks if the condition is true (values need to be switched) and adds the values to two separate dataframes. If the condition is not true I add the original values to df1 and df2. At the end of the block, I was planning on combining the dataframes together again.
However, both of these methods take super long to run, and it seems there has to be something more efficient. I had trouble finding the most efficient way online, which one would it be?
METHOD 1:
finalGameID = list(final.loc[:,'GameID'])
for i,v in enumerate(finalGameID):
if final['HomeAway'][i] == 0:
print(v)
values = final.loc[i].values
value1 = list(values[4:124])
value2 = list(values[124:])
final.iloc[i,4:124] = value2
final.iloc[i,124:] = value1
METHOD 2:
df1 = final[final.columns[4:124]]
df2 = final[final.columns[124:]]
df3 = final[final.columns[0:4]]
df1 = df1[0:0]
df2 = df2[0:0]
finalGameID = list(final.loc[:,'GameID'])
for i,v in enumerate(finalGameID):
values = final.loc[i].values
value1 = list(values[4:124])
value2 = list(values[124:])
if final['HomeAway'][i] == 0:
print(v)
df1.loc[len(df1.index)] = value2
df2.loc[len(df2.index)] = value1
else:
df1.loc[len(df1.index)] = value1
df2.loc[len(df2.index)] = value2