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
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.
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.
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.
You should use @variable_name
with @
query=df2.query('cc_vehicle_line==@variable_name')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With