0

I have a dataframe such as: store_id, store_name, and number_of_orders:

    store_id                        name cbpr_ordes
0  900004468  [F] | Starbucks | STAR 013          1
1  900038719      Julice Boulangere Pães          1

And for each row I would like to print something like this: "Hello, the (store_id) had (number_of_orders) in the last 15 minutes"

I tried this:

for row in df:
    print("Hello, the store_id " + str(df['store_id']) + " had " + str(df['cbpr_ordes']))

But I got:

Hello, the store_id 0    900004468
1    900038719
Name: store_id, dtype: object had 0    1
1    1
Name: cbpr_ordes, dtype: object
Hello, the store_id 0    900004468
1    900038719
Name: store_id, dtype: object had 0    1
1    1
Name: cbpr_ordes, dtype: object
Hello, the store_id 0    900004468
1    900038719
Name: store_id, dtype: object had 0    1
1    1
Name: cbpr_ordes, dtype: object
martineau
  • 119,623
  • 25
  • 170
  • 301
Leandro B
  • 3
  • 1
  • 1
    Does this answer your question? [How to iterate over rows in a DataFrame in Pandas](https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas) – High-Octane Jun 14 '20 at 14:31
  • 1
    Just a reminder, f-string is just good for this case, change line inside print() into this: f"Hello, the store_id {df['store_id']} had {df['cbpr_orders']}", no need to hassle with '+' and 'str()'. – jupiterbjy Jun 14 '20 at 14:33

1 Answers1

0

Use iterrows() function:

for index, row in df.iterrows(): 
    rows = dict(row)
    print("Hello, the store_id " + str(rows['store_id']) + " had " + str(rows['cbpr_ordes']))
High-Octane
  • 1,104
  • 5
  • 19