I need to create messages of the kind Hi John, it's time to eat quantity_1 of food1, quantity_2 of food_2 ..., qunatity_n of food_n.
For that I get pandas dataframes, that update every once in a while. For example the dataframes look sometimes like df1=
qantity,food
1,apple
and sometimes like df2=
quantity,food
1,apple
3,salads
2,carrots
I need with every new update to create out of the dataframe a string for a message. For df1
an f-string works neetly and for creating my desired message of Hi John, it's time to eat 1 apple.
I can do:
f"Hi John, it's time to eat {df.quantity} {df1.food}
In the cases when I have something like df2
, and I don't know explicitly how long df2
is, I would like to have a message of the kind Hi John, it's time to eat 1 apple, 3 salads, 2 carrots.
How can I create such a string? I thought of using the "splat" operator for something like join(*zip(df.quantity, df.food))
, but I haven't figured it out. tnx
Try this:
result=','.join([str(i[0])+' '+i[1] for i in zip(df.quantity, df.food)])
print(result)
'1 apple, 2 salads, 3 carrots'
And you can add this to get the final result:
"Hi John, it's time to eat " + result
Hi John, it's time to eat 1 apple, 2 salads, 3 carrots