I have a simple dataframe and would like to apply a function to a particular column based on the status of another column.
myDF = pd.DataFrame({'trial': ['A','B','A','B','A','B'], 'score': [1,2,3,4,5,6]})
I would like to multiply each observation in the score column by 10, but only if the trial name is 'A'. If it is 'B', I would like the score to remain the same.
I have a working version that does what I need, but I'm wondering if there is a way I can do this without having to create the new dataframe. Not that it makes a huge difference, but eight lines of code seems pretty long and I'm guessing there might be a simpler solution I have overlooked.
newDF = pd.DataFrame(columns = ['trial','score'])
for row in myDF.iterrows():
if row[1][0] == 'A':
newScore = {'trial': 'A', 'score': row[1][1]*10}
newDF = newDF.append(newScore, ignore_index=True)
else:
newScore = {'trial': 'B', 'score': row[1][1]}
newDF = newDF.append(newScore, ignore_index=True)