I am iterating over a pandas table using the itertuples() iterator function. I would like to set a value in another column when a condition is True. Thats easy. But I would like to set another value based on the previously set value to another column again and thats not working. I have to iterrate a second time to do that, but this is inefficient. How can i set multiple values in different columns within one iteration process.
Here is some example code:
data = {
'Animal': ['cat', 'dog', 'dog', 'cat', 'bird', 'dog', 'cow'],
'Noise': ['muh', 'miau', 'wuff', 'piep', 'piep', 'miau', 'muh']
}
df = pd.DataFrame(data)
df.insert(loc=2, column='Match', value='')
df.insert(loc=3, column='Comment', value='')
for row in df.itertuples():
if row.Animal == 'cat' and row.Noise == 'miau':
df.set_value(index=row.Index, col='Match', value=True)
elif row.Animal == 'dog' and row.Noise == 'wuff':
df.set_value(index=row.Index, col='Match', value=True)
elif row.Animal == 'bird' and row.Noise == 'piep':
df.set_value(index=row.Index, col='Match', value=True)
elif row.Animal == 'cow' and row.Noise == 'muh':
df.set_value(index=row.Index, col='Match', value=True)
# Why is this not getting applied to the 'Comment' column?
if row.Match is True:
df.set_value(index=row.Index, col='Comment', value='yeah')
I have to do another iteration instead to get the Comment-column filled:
for row in df.itertuples():
if row.Match is True:
df.set_value(index=row.Index, col='Comment', value='yeah')
But with i.e. 500000+ values this is very inefficient and time consuming. So what is a better way to do something like that?
or. That way, you don't need to keep repeating codedf.set_value(index=row.Index, col='Match', value=True) df.set_value(index=row.Index, col='Comment', value='yeah'). But the below is a much better answer