0

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 !

2
  • 1
    Is the dtype of Donor_Age str or numeric? If numeric you need to cast the type of donor_age as str is default type from input Commented May 25, 2018 at 8:25
  • Yes it worked..thanks Ed. Commented May 25, 2018 at 8:53

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

yes did the same..also for the string input I had to follow the same process. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.