简体   繁体   中英

Python, Pandas - How can I get something printed in a data range?

I suppose to create a function that allows user pick a range and it will print out the number within the range. however, I keep getting empty DataFrame with my code. can anyone help me?

` import pandas as pd

if __name__ == "__main__":   
file_name = "sales_rossetti.xlsx"    

# Formatting numbers (e.g. $1,000,000)
pd.options.display.float_format = '${:,.0f}'.format

# Reading Excel file
df = pd.read_excel(file_name, index_col = 0, convert_float = False)
print ("Welcome to Rossetti's Sales program\n")  
print ("1) Search by State")
print ("2) Search by Jan Sales")
print ("3) Search by Q2 sales")
print ("4) Exit")

my_option = input ("Please select a menu option:")


if (my_option=="2"):
    my_columns = ["Name", "City", "State", "Jan"]
    your_sales = input("please enter the minimum sale: ")
    your_sales = input("please enter the maxium sale: ")
    print (df[my_columns][df.Jan>int(your_sales)][df.Jan<int(your_sales)])`

You're overwriting the your_sales variable as you're reusing it, so you should use a different variable name for the min and max params. You then need to generate a proper boolean mask using loc and enclosing your boolean conditions using parentheses and & to and the array of boolean values:

if (my_option=="2"):
    my_columns = ["Name", "City", "State", "Jan"]
    min_sales = input("please enter the minimum sale: ")
    max_sales = input("please enter the maxium sale: ")
    print (df.loc[(df.Jan > int(min_sales) ) & (df.Jan < int(max_sales)), my_columns])

the above should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM