1

I am having a for-loop which takes rows within a pandas dataframe df_drinks and uses them as parameters to call another function order(). order() is imported from the module restaurant.py.

Together with the row in df_drinks, I want to submit a comment to the order(), which is specified outside the for-loop.

from restaurant import order

statement = "Lets order"
df_drinks = ["1 drink", "2 drink"] # simplified, 1 item per row, many columns

for index, row in df_drinks.iterrows():
    print ("%s, %s" % (statement, row))
    item = row
    response = order(statement, item)
    ...

The module looks like this:

# restaurant.py

def order(statement, item):
    listen(statement)
    statement = "order received"
    ready_drinks = prepare(item)
    ...
    return ready_drinks

For the first run/row everything is fine since a print yields:

Lets order 1 drink

However, for the second run/row, print yields:

order received 2 drinks instead of Lets order 2 drinks.

I understand that I have the same variable name statement for two different things. Still, I am confused since order() in restaurant.py does only return ready_drinks and not statement.

How to correctly assign local variables to a for-loop in python?

sudonym
  • 3,788
  • 4
  • 36
  • 61

3 Answers3

1

statement = "order received" is a local variable and statement = "Lets order" is a Global variable. You are no where overriding the value in the for loop. May be below code helps you

df_drinks = ["1 drink", "2 drink"] # simplified, 1 item per row, many columns

for row in df_drinks:
    statement = "Lets order"
    print ("%s, %s" % (statement, row))
    item = row
    ready_drinks,statement = order(statement, item)
    print ("%s, %s" % (statement, row))


def order(statement, item):
    listen(statement)
    statement = "order received"
    ready_drinks = prepare(item)
    ...
    return ready_drinks,statement
Sandeep
  • 91
  • 5
0

I am not sure what are you trying to ask but it appears this might be due to indentation if I am understanding correctly what you are saying so the code becomes.

from restaurant import order

statement = "Lets order"
df_drinks = ["1 drink", "2 drink"] # simplified, 1 item per row, many columns

for index, row in df_drinks.iterrows():
    print ("%s, %s" % (statement, row))
    item = row
response = order(statement, item)
Daniyal Ahmed
  • 715
  • 5
  • 11
0

Many thanks for the answers. I over-simplified. What should have been mentioned initially is that statement is not a string in the original code, but a pandas df. According to my understanding, this led to issues regarding Pandas: Chained assignments .

The statement dataframe undergoes several inplace = True processing steps in restaurants.py after the first row in the for loop of the first code. This led to the above-mentioned changes in all other rows of the for loop.

In case someone else may face a similar problem in the future, I want to add how to prevent this: Creating one seperate copy of the dataframe for the group of operations in restaurants.py, as described here.

statement2 = statement.copy(deep=False)
sudonym
  • 3,788
  • 4
  • 36
  • 61