Lets say I have a the following pandas data frame with the following columnar structure and the dataframe is titled df
index column1 column2 column3
0 2 5 apple
1 4 3 apple
2 6 1 orange
3 8 6 apple
4 10 5 orange
I would like to search the dataframe such that it will recognize every row where df['column3'] == orange and extract the value of df['column1'] and df['column2'] in that row and insert it into the below function and then change the existing value of df[column2'] by the output of the function.
def func(x, y):
return x * 2.0
Thus far I have implemented the following, which works, but I suspect it is not the most pythonic way of doing this, and probably does not have the most efficient execution speed. Any advice would be appreciated.
for i in range(len(df.index)):
if df.loc[i, 'column3'] == 'orange':
df.loc[i, 'column2'] = func(df.column1, df.column2)