Hi @danta, it would help us to read the code if you surround your code with backticks.
I think a neater option would be to use Pandas to create your new condition file at the beginning of the experiment, and load it into trial handler in Builder for randomisation. In a code component, in the “Begin Experiment” tab:
import pandas as pd
fileName = ['neutral.csv', 'threat1.csv', 'threat2.csv']
conditionFile = pd.DataFrame(columns=['stim']) # where 'stim' is the column name for your images
for files in fileName:
myDat = pd.read_csv(files) # Get conditions file
myDat = myDat.sample(frac=1).reset_index(drop=True) # Shuffle the dataframe
myDat = myDat.head(20) # Take first 20 rows from sheet
conditionFile = conditionFrame.append(myDat) # Append to new conditions file variable
conditionFile.to_csv("myStim.csv") # Save to new csv file for use in the loop handler.