I'd like to use the .query method to filter a column in a dataframe by a variable but it won't work with a variable, only a string. Anybody know how to make it work with a variable? Thank you.
import pandas as pd
var="A"
source = {'COL1': ['A','B','C'], 'COL2': ['D','E','F']}
dfsource=pd.DataFrame(source)
print(dfsource)
df2=dfsource.query('COL1=="A"') #Example, this works filtering for value A but not what I need.
df3=dfsource.query('COL1'==var)
print(df2)
print(df3)