2016-09-15 61 views
0

我想创建一个功能,允许用户选择一个范围,它会打印出范围内的数字。但是,我一直使用我的代码获取空的DataFrame。谁能帮我?Python,熊猫 - 我怎样才能在数据范围内打印某些东西?

`进口大熊猫作为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

那么首先你使用相同变量为你的最小和最大范围值'your_sales'被覆盖,所以你应该使用不同的变量,然后你想'print(df.loc [(df.Jan> int(min_sales))&(df.Jan EdChum

+0

您正在将最小和最大销售额保存到同一变量。它必须是两个不同的变量。 –

回答

0

你覆盖your_sales变量,你重用它,所以你应该为最小值和最大值PARAMS使用不同的变量名。然后,您需要使用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]) 

上面应该工作

+0

谢谢,我在找什么。 –

+0

如果我的答案解决了你的问题,那么你可以接受它,我的答案左上角会有一个空的刻度标记 – EdChum

相关问题