I have the following pandas dataframe
import pandas as pd
foo = pd.DataFrame({'source': ['tomato', 'carrots', 'cheese'],
'tomato': [0.7, 0.4, 0.8],
'carrots': [0.15,0.3,0.1],
'cheese': [0.15,0.3,0.1]})
foo
source tomato carrots cheese
0 tomato 0.7 0.15 0.15
1 carrots 0.4 0.30 0.30
2 cheese 0.8 0.10 0.10
and the following list:
sequence = ['carrots','carrots','tomato','carrots']
The sequence describes the sequential steps: carrots -> carrots -> tomato -> carrots, which means going from carrots to carrots, then going from carrots to tomato and lastly going from tomato to carrots.
I would like, to iterate over foo, according to the above sequence, and calculate
foo['carrots']['carrots']*foo['carrots']['tomato']*foo['tomato']['carrots']
where foo['carrots']['carrots'] is the element of foo that corresponds to source carrots and column carrots, so 0.3
How could I achieve that efficiently ?