Jul-16-2019, 12:30 AM
(This post was last modified: Jul-16-2019, 12:30 AM by scionsamurai.)
My goal is to put together a pandas column search function based on how many columns are present.
Would your suggestion work if searching the below in the example data frame above?
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'