[英]The code works fine outside the function but once inside, it doesn't work
[英]Python code works outside function, but doesn't work inside function
我正在处理一些数据,仅按顺序编写行就可以正常工作,并为我提供了想要的结果(从数据框“餐厅”中提取日期的数据行):
orders = restaurant[(restaurant.index == date)]
但是,当我将其放入函数中时,它不再能够按日期查找它,而是给了我一个空白的数据框:
def datesearch(date)
orders = restaurant[(restaurant.index == date)]
return orders
我似乎无法弄清楚为什么在函数外还可以,但是由于某种原因,当我将其放入函数中时无法按日期进行搜索。
我认为restaurant
是一个全局变量,因此它可能未使用正确的数据。 尝试这个:
def datesearch(date)
global restaurant
orders = restaurant[(restaurant.index == date)]
return orders
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.