Python Forum
Passing a int into the query
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing a int into the query
#1
Hi, i have an input on my code and I want that this input pass a int to the query.
For example I type ‘1’ and it search by the number typed.

the code i have

import pandas as pd
 
df = pd.read_excel("book.xlsx")

ser = input("Provide age: ")


df_val = df.query(f"id==1")['Name']





print(df_val.values[0])  
Thanks
Reply
#2
By default, the input function will return a string, but you can do an inline type conversion.

This code is a demonstration of that.

x = input("Enter a number: ")

print(f"x = {x} and is",type(x))

x = int(input("Enter a number: "))

print(f"x = {x} and is",type(x))
devilonline likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
It will be like this,don't need to pass int as df.query only take string and then evaluate it to right type.
import pandas as pd

df = pd.read_excel("book.xlsx")
ser = input("Provide age: ")
df_val = df.query(f"id=={ser}")['Name']

print(df_val.values[0])
devilonline likes this post
Reply
#4
thanks both its works perfectly
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 750 Dec-04-2023, 09:48 PM
Last Post: sanky1990

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020