Interesting problem while searching a row in pandas using variables instead of literal

Question:

I am running the python code

import pandas as pd
print("***Welcome to Bike Rental System***")
n=input("Enter number of persons: ")
both=input("Enter number of females who are senior citizens(both category): ")
females=input("Enter the number of females who are not senior ciizen(senior category): ")
senior=input("Enter the number of people who are not females and are senior citizens(senior category): ")
hours=input("Enter the number of hours to rent: ")
prices=pd.read_csv('prices.csv')
#mask=(prices['Number of Persons']==n)#&(prices['Both']==both )&(prices['No. of females']==females)&(prices['No. of Senior Citizens']==senior)
desired_column=prices[prices['Number of Persons']==2]
print(n)
print(desired_column)

with the prices.csv as

Number of Persons,No. of Senior Citizens,No. of females,Both,Base Price (in rs/hr),Discount %,net_price,Blueprint
1,0,0,0,50,0,50,N
1,1,0,0,50,10,45,S
1,0,1,0,50,5,47.5,F
1,0,0,1,50,20,40,B
2,0,0,0,100,0,100,NN
2,1,0,0,100,5,95,NS
2,0,1,0,100,2.5,97.5,NF
2,0,0,1,100,10,90,NB
2,2,0,0,100,20,80,SS
2,1,1,0,100,15,85,SF
2,1,0,1,100,25,75,SB
2,0,2,0,100,10,90,FF
2,0,1,1,100,20,80,FB
2,0,0,2,100,30,70,BB

which works fine and outputs all the rows with Number of persons with 2 as the value.
But the problem is when I use desired_column=prices[prices['Number of Persons']==n] instead of desired_column=prices[prices['Number of Persons']==2] and enter 2 as the number of persons (n),the latter returns an empty dataframe.
Thanks for the help.

Asked By: Sourabh Yadav

||

Answers:

Pandas is considering the number of persons as an integer and hence not equating it to a string.

Answered By: Sourabh Yadav
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.