简体   繁体   中英

Python code works outside function, but doesn't work inside function

I'm working with some data, and just writing lines in sequence works fine and gives me the results I want (to extract a row of data from the date from the dataframe 'restaurant'):

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

However, when I put this into a function, it no longer is able to look it up by date, and instead just gives me a blank data frame:

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

I can't seem to figure out why it's fine outside the function, but for some reason, it can't search by the date when I put it in a function.

I think restaurant is a global variable, so it might not be using the correct data. Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM