提问者:小点点

如何打印每个DataFrame的行迭代消息?


我有一个数据框架,例如:store_idstore_namenumber_of_orders:

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

对于每一行,我希望打印如下内容:“Hello,the(store_id)have(number_of_orders)in the last 15 minutes”

我试了一下:

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

但我得到了:

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

共1个答案

匿名用户

使用iterrows()函数:

for index, row in df.iterrows(): 
    rows = dict(row)
    print("Hello, the store_id " + str(rows['store_id']) + " had " + str(rows['cbpr_ordes']))