I wanted to use external variable with the query method in a pandas dataframe. Below is my code
import pandas as pd
df1 = pd.DataFrame({'col1' : ['AA', 'BB', 'CC'], 'col2' : [2,3,4], 'col3' : [10,11,12]})
ii = 'BB'
pd.concat([pd.DataFrame([[df1.query("col1 == @ii").reset_index(drop = True).iloc[0, 1] * jj for jj in range(0,3)],
[df1.query("col1 == @ii").reset_index(drop = True).iloc[0, 2] + jj for jj in range(0,3)]]).T for ii in df1['col1'].values], axis = 1)
The result looks strange to me. I used a variable ii within query method which supposed to change based on different values of col1. But it appears to take only the value ii = 'BB' which is defined externally.
Is there any explanation to this behavior? How can I change the value of ii within query method?
@iiis not working in this case