Kishanu Bhattacharya
Kishanu Bhattacharya

Reputation: 97

Using User Input as a Where Clause in Pandas Dataframe

I have a requirement where I need to apply some aggregate functions and filter the values based on some user input Take a look at the code below:

import pandas as pd
import numpy as np
from pandas import ExcelWriter
import tensorflow

donor_age = input("Enter Donor's Age: ")
donor_organ = input("Enter Organ to Donate: ")

writer = pd.ExcelWriter('Probability.xlsx', engine='xlsxwriter')
workbook = writer.book
df_success_rates = pd.read_excel('HealthChain.xlsx')

df_total=df_success_rates.groupby(['Donor_Age','Donor_Gender','Donor_Organ','SuccessfullCompletion'])\
    .size().to_frame('total_count').reset_index().sort_values(['Donor_Age', 'total_count'], ascending=True)
df_total['Total_Operations']=df_total.groupby(['Donor_Age','Donor_Gender','Donor_Organ']).total_count.transform(np.sum)

df_total['Probability']=df_total['total_count']/df_total['Total_Operations']
#df_total=df_total[(df_total.Donor_Age == donor_age)]
df_total.to_excel(writer, sheet_name='Prob', engine='xlsxwriter', index=False)

writer.save()
workbook.close()

Everything works well but when the complier reads the line

df_total=df_total[(df_total.Donor_Age == donor_age)]

it throws an error stating enter image description here

But when the donor's age is hardcoded, the code runs well.

Any idea what could be the reason for the issue and the fix for the same

Thanks in Advance !

Upvotes: 0

Views: 341

Answers (1)

Ontamu
Ontamu

Reputation: 201

Do

donor_age = int(input("Enter Donor's Age: "))

input() returns a string, it looks like you are comapring a string with a int which can't be compared. Either cast the input to a int or float or cast the other age as a str.

Upvotes: 1

Related Questions