Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python代码在函数外部工作,但不';不能在函数内部工作_Python_Pandas_Dataframe - Fatal编程技术网

Python代码在函数外部工作,但不';不能在函数内部工作

Python代码在函数外部工作,但不';不能在函数内部工作,python,pandas,dataframe,Python,Pandas,Dataframe,我正在处理一些数据,只需按顺序写几行就可以了,并给出我想要的结果(从数据框“餐厅”的日期中提取一行数据): 但是,当我将其放入函数中时,它不再能够按日期查找,而只会给我一个空白数据框: def datesearch(date) orders = restaurant[(restaurant.index == date)] return orders 我似乎不明白为什么它在函数之外是好的,但出于某种原因,它无法按我在函数中放置它的日期进行搜索。我认为餐厅是一个全局变量,因此

我正在处理一些数据,只需按顺序写几行就可以了,并给出我想要的结果(从数据框“餐厅”的日期中提取一行数据):

但是,当我将其放入函数中时,它不再能够按日期查找,而只会给我一个空白数据框:

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

我似乎不明白为什么它在函数之外是好的,但出于某种原因,它无法按我在函数中放置它的日期进行搜索。

我认为
餐厅
是一个全局变量,因此它可能没有使用正确的数据。试试这个:

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

你要把日期传给活动吗?很抱歉,那是什么意思?我需要先将date=date写入函数吗?您可以调用datesearch作为
datesearch(date)
。你收到错误信息了吗?没有-我没有收到错误。相反,我得到一个空白的数据帧。我的函数似乎找不到“日期”行。嗯,好的。您是否正在捕获函数的返回值?换句话说,
orders=datesearch(date)
?我不明白这怎么可能解决这个问题,因为您只需要在修改名称时声明一个全局名称。
def datesearch(date) 
    global restaurant  
    orders = restaurant[(restaurant.index == date)]
    return orders