Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 678
serphaes Avatar asked Apr 12 '26 11:04

serphaes


1 Answers

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
like image 57
Kevin London Avatar answered Apr 15 '26 02:04

Kevin London



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!