2014-09-12 86 views
0

我正在處理一些數據,並且只是在順序中編寫行能夠正常工作,並且給了我想要的結果(從日期提取一行數據數據框「餐廳」):Python代碼在函數外部工作,但在函數內部不起作用

orders = restaurant[(restaurant.index == date)] 

然而,當我把這個變成一個功能,它不再能夠看它的日期,而不是隻給我一個空白數據幀:

def datesearch(date) 
    orders = restaurant[(restaurant.index == date)] 
    return orders 

我似乎無法弄清楚爲什麼它在函數外很好,但由於某種原因,它不能通過d吃了,當我把它放在一個函數。

+0

你是否將日期傳遞給函數? – 2014-09-12 21:06:31

+0

我很抱歉,但這是什麼意思?我是否需要先將date = date寫入我的函數? – serphaes 2014-09-12 21:07:09

+0

你可以將datesearch稱爲'datesearch(date)'。你是否收到錯誤信息? – 2014-09-12 21:07:39

回答

1

我認爲restaurant是一個全局變量,所以它可能沒有使用正確的數據。試試這個:

def datesearch(date) 
    global restaurant 
    orders = restaurant[(restaurant.index == date)] 
    return orders 
+0

非常感謝你!這解決了我的問題! – serphaes 2014-09-12 21:18:12

+4

我不明白這是如何解決這個問題的,因爲如果你修改它,你只需要聲明一個全局名稱。 – kindall 2014-09-12 21:32:20

相關問題