0

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?

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.