Python Forum

Full Version: Passing a int into the query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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))
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])
thanks both its works perfectly