Python Forum
Looking for Eval alternative
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looking for Eval alternative
#1
My goal is to put together a pandas column search function based on how many columns are present.
import pandas as pd

head_list = 'Segment', 'Country', 'Discount Band'
search_vals = 'Government', 'France', 1899
data = [['Government', 'France', 1899], ['Midmarket', 'France', 1899], ['Midmarket', 'Mexico', 1899]]
df = pd.DataFrame(data, columns = ['Segment', 'Country', 'Discount Band'])
if len(head_list) > 1:
    exec_str = 'df['
    for x in head_list:
        if head_list.index(x) != (len(head_list) - 1):
            exec_str += '(df[head_list[' + str(head_list.index(x)) + ']] == search_vals[' + str(head_list.index(x)) + ']) &'
        else:
            exec_str += '(df[head_list[' + str(head_list.index(x)) + ']] == search_vals[' + str(head_list.index(x)) + '])]'

print(eval(exec_str))
^^ So far i was using something like this but was reading that eval isn't suggested when accepting user input. Does anyone have suggestions for how to handle this differently?
Would your suggestion work if searching the below in the example data frame above?
head_list = 'Segment', 'Country'
search_vals = 'Government', 'France'
Reply


Messages In This Thread
Looking for Eval alternative - by scionsamurai - Jul-16-2019, 12:30 AM
RE: Looking for Eval alternative - by scidam - Jul-16-2019, 02:23 AM
RE: Looking for Eval alternative - by scionsamurai - Jul-16-2019, 02:41 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020