Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use variables inside query in Pandas?

I have problem quering the data frame in panda when I use variable instead of value.

df2 = pd.read_csv('my.csv')
query=df2.query('cc_vehicle_line==7')

works fine but

df2 = pd.read_csv('my.csv')
query=df2.query('cc_vehicle_line==variable_name')

It throws the message that variable_name is undefined.But it is defined. I cannot use hardcoded value as I need to automate and depending of value of variable_name, select relevant rows.

Am I missing something?

Thanks

like image 576
user437777 Avatar asked May 20 '15 04:05

user437777


People also ask

Is query faster than LOC?

The query function seams more efficient than the loc function. DF2: 2K records x 6 columns. The loc function seams much more efficient than the query function.

How do you declare variables in pandas?

If you want to add multiple variables, you can do this with a single call to the assign method. Just type the name of your dataframe, call the method, and then provide the name-value pairs for each new variable, separated by commas. What is this? Honestly, adding multiple variables to a Pandas dataframe is really easy.

What does .values in pandas do?

It can be thought of as a dict-like container for Series objects. This is the primary data structure of the Pandas. Pandas DataFrame. values attribute return a Numpy representation of the given DataFrame.


1 Answers

You should use @variable_name with @

query=df2.query('cc_vehicle_line==@variable_name')
like image 70
Zero Avatar answered Oct 10 '22 16:10

Zero