Python Forum
Create selection box to pass string value based on uniques in Excel column - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Create selection box to pass string value based on uniques in Excel column (/thread-14088.html)



Create selection box to pass string value based on uniques in Excel column - sneakysnek - Nov-14-2018

Hello!

New to python but I'm trying, all advice is appreciated.

I have a master data sheet which I have loaded into a data frame. I have then been able to hard-code a value from the "Delivery BA" column in to return a data frame with only that value. I have also been able to list and count the unique values in the column. I currently have a cell which displays a string value of the filter criteria selected for the "Delivery BA" column.

What I would IDEALLY like to do is to be able to prompt the user with a selection box containing all the unique values in the column, and then, upon the user selecting a value, pass that value to the dataframe parameters so that it creates a dataframe with only that "Delivery BA" for entries, preferably without needing to access the value passed in the filter string cell.

I have searched and searched so I apologize if this has been answered already, or something similar.

Please see what little code I have so far.

Explanation of variables:
d: The number of entries in the column "Delivery BA"
g: Not sure what I was doing here; I believe trying to identify the column as "Delivery BA"
h: Entries for a particular company in the column
j: Accessing the cell containing the string value of the selected filter criteria
q: This didn't do what I wanted; I'm not sure now what I wanted
x: Unique values in "Delivery BA" column
y: The number of unique values in "Delivery BA" column. At time of writing, it sits at 739.
    import pandas
    
    data = pandas.read_excel('Test Master.xlsx', header = None).values
    
    print(data)
    df = pandas.DataFrame(data)
    new_header = df.iloc[0]
    df = df[0:]
    df.columns = new_header
    
    d = df['Delivery BA'].value_counts()
    print(d)
    
    g = df.groupby('Delivery BA', as_index = False)
    print(g)
    
    h = df.loc[df['Delivery BA']== 'AmeriGas Propane, L.P.']
    print(h)
    
    j = df.iloc[0, 25]
    print(j)
    
    q = df.loc[df['Delivery BA'] == j]
    
    print(q)
    
    x = df['Delivery BA'].unique()
    print(x)
    y = df['Delivery BA'].nunique()
    print(y)
Apologies if I've posted anything arbitrary or not enough information. Thank you again.


RE: Create selection box to pass string value based on uniques in Excel column - Stefanovietch - Nov-18-2018

You can do something like this, add it at the end of the code. If you run python and type d,g,h etc. and enter the select values will be print
def menu():
    choice = input("""What variable do you want to see?
(Choose between: d, g, h, j, q, x and y)

Enter your choice:""")
    if choice == "d":
        print(d)
    elif choice == "g":
        print(g)
    #Etc...
    
menu()
Got some inspiration at this link, maybe its helpfull:http://www.teachingcomputing.com/learn.php?a=06_Mini_Projects_visit_www_teachyourselfpython_com&t=01_Form_Tutor_System_NEA_OCR_Task1&s=02_MainMenu_Define_Functions