我想创建一个允许用户选择范围的功能,它会打印出该范围内的数字。但是,我一直用我的代码获取空DataFrame。谁能帮我?
`将pandas导入为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)])`
答案 0 :(得分:0)
当您重复使用时,您将覆盖your_sales
变量,因此您应该为min和max参数使用不同的变量名称。然后,您需要使用loc
生成一个正确的布尔掩码,并使用括号括起您的布尔条件,并将&
括起来and
布尔值数组:
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])
以上应该有效