I want to iterate through every row in a pandas DataFrame, and do something with the elements in each row.
Right now I have
for row in df.iterrows():
if row['col'] > 1.5:
doSomething
but it tells me that the 'tuple indices must be integers, not str' . How do I access the column that I want in a certain row?
df.loc[df['col']>1.5, 'col'] = doSomething
would achieve the same result and will be blisteringly fast as it will be vectorisednp.where
or just 2 statements so eitherdf['col'] = np.where(df['col'] > 1.5, doSomething, doSomethingElse)
or add another statement for the opposite conditiondf.loc[df['col'] <=1.5, 'col'] = doSomethingElse