Home > database >  Operator with 3 variables highest variable show name with pandas and python
Operator with 3 variables highest variable show name with pandas and python

Time:11-15

I'm trying to obtain the variable with the highest number so basically this are the variables:

number_no = 17
number_yes = 2
number_dontknow = 10 

I would like to know with one is the highest I was using max(number_no, number_yes) but it gave me the number and I need the variable name, so I would like to have something like:

highest_variable = max(number_no, number_yes)

if highest_variable == number_no:
   total = sum(number_no   number_yes   number_dontknow)
   percentage = number_no/total
   print(percentage  "%")
   #Show percentage to use it in another function
if else highest_variable == number_yes:
   total = sum(number_no   number_yes   number_dontknow)
   percentage = number_yes/total
   print(percentage  "%")
   #Show percentage to use it in another function

CodePudding user response:

I'm not sure if I understand you correctly?

But if I understand correctly:

  • The 1st solution is that use max rather the variable:
total = sum(number_no   number_yes   number_dontknow)
percentage = max(number_no, number_yes)/total
print(percentage  "%")
#Show percentage to use it in another function
  • The 2nd solution is that swap sort numbers to sure that one if highest. In this code We could be sure that number_yes is highest:
if number_no > number_yes:
  number_yes , number_no = number_no , number_yes

total = sum(number_no   number_yes   number_dontknow)
percentage = number_yes/total
print(percentage  "%")
#Show percentage to use it in another function
  • I don't understand why you use sum()? I think you mean:
total = number_no   number_yes   number_dontknow

CodePudding user response:

If you are trying to get the highest name and highest value:

number_no = 17
number_yes = 2
number_dontknow = 10 

numbers_name = ["number_no","number_yes","number_dontknow"]
numbers_value = [number_no,number_yes,number_dontknow]
highest_value = max(number_no,number_yes,number_dontknow)
highest_name = numbers_name[numbers_value.index(highest_value)]

print(highest_name,highest_value)

# output: ('number_no', 17)
  • Related