[英]How can I take input from something the Console has printed in Python?
[英]Python, Pandas - How can I get something printed in a data range?
我想創建一個允許用戶選擇范圍的函數,它將打印出該范圍內的數字。 但是,我的代碼不斷獲取空的DataFrame。 誰能幫我?
`將熊貓作為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)])`
重復使用時,您將覆蓋your_sales
變量,因此,應為min和max參數使用不同的變量名稱。 然后,您需要使用loc
生成適當的布爾掩碼,並使用括號和&
to 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])
以上應該工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.