Home > Enterprise >  How to solve Invalid comparison?
How to solve Invalid comparison?

Time:02-08

def show_entry_fields():
    print("Bit Diameter as cms: %s\nWell Name: %s" % (e1.get(), e2.get()))

master = tk.Tk()
tk.Label(master, 
         text="Bit Diameter as cms").grid(row=0)
tk.Label(master, 
         text="Well Name").grid(row=1)

e1 = tk.Entry(master)
e2 = tk.Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

tk.Button(master, 
          text='Quit', 
          command=master.quit).grid(row=3, 
                                    column=0, 
                                    sticky=tk.W, 
                                    pady=4)
tk.Button(master, 
          text='Show', command=show_entry_fields).grid(row=3, 
                                                       column=1, 
                                                       sticky=tk.W, 
                                                       pady=4)

tk.mainloop()


import tkinter.filedialog

file = filedialog.askopenfilename()

if file:
        try:
            filename = r"{}".format(file)
            df = pd.read_excel(file)
        except ValueError:
            label.config(text="File could not be opened")
        except FileNotFoundError:
            label.config(text="File Not Found")
                         


data = df.loc[df['WELL'] == 'V131B'][['DEPTH_MD', 'CALIPER', 'GR', 'LITHOLOGY', 'SHALLOW', 'DEEP']]

df.loc[(df.GR < 30.0000) & (df.CALIPER > 'e1') & (df.SHALLOW > 2.60000)  , "LITHOLOGY"] = '1' 
df.loc[(df.GR > 100.0000) , "LITHOLOGY"] = '3' 
df.LITHOLOGY.fillna('2', inplace=True)

df


app_root.mainloop()

I'm trying to create an interface for taking data from user. As you can see, I want user to write a value which I defined as e1. The problem is when I try to connect this with my data frame, It gives TypeError: Invalid comparison between dtype=float64 and str .

How can I solve the problem in this line? df.loc[(df.GR < 30.0000) & (df.CALIPER > 'e1') & (df.SHALLOW > 2.60000) , "LITHOLOGY"] = '1'

Edit: I changed the type of e1 as print("Bit Diameter as cms: %s\nWell Name: %s" % (float(e1.get()), e2.get())) but still I have problems. I cannot use e1 as I want.

CodePudding user response:

As @cls said you are comparing a float to the string 'e1' in (df.CALIPER > 'e1'). You need to compare df.CALIPER to float( e1.get() ). I don't have access to your files so I've not shown a second gui just a print statement. As the first GUI will be destroyed after the quit e1.get() must be executed before destroying the application with the result stored for later access.

import tkinter as tk

def show_entry_fields():
    print("Bit Diameter as cms: %s\nWell Name: %s" % (e1.get(), e2.get()))

master = tk.Tk()
tk.Label(master, 
         text="Bit Diameter as cms").grid(row=0)
tk.Label(master, 
         text="Well Name").grid(row=1)

e1 = tk.Entry(master)
e2 = tk.Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

e1_float = 0.0          # Storage for results on exit
e2_string = ""          # Storage for results on exit

def get_float( entry ):
    """ Converts the characters in an Entry to a float, 
        returns 0.0 if not a valid float"""
    try: 
        return float( entry.get() )
    except ValueError:
        return 0.0

def save_and_quit():
    global e1_float, e2_string
    e1_float = get_float( e1 )
    e2_string = e2.get()
    print( type( 'e1'),  'e1', type(e1), e1 )   # Prints to see what happens.
    print( type( e1.get()), e1.get(), type(e1_float), e1_float )
    master.destroy()

tk.Button(master, 
          text='Save & Quit', 
          command=save_and_quit).grid(row=3, 
                                      column=0, 
                                      sticky=tk.W, 
                                      pady=4)
tk.Button(master, 
          text='Show', command=show_entry_fields).grid(row=3, 
                                                       column=1, 
                                                       sticky=tk.W, 
                                                       pady=4)

tk.mainloop()

print("Bit Diameter as cms: %s\nWell Name: %s" % (e1_float, e2_string))

CodePudding user response:

You may be comparing the string 'e1' to a float value. You should give a float value to what you call 'e1' in "(df.CALIPER > 'e1')"

  •  Tags:  
  • Related