Reputation: 419
I have a dataframe which includes names, age and score. What I'm trying to do is pass the name, age and score into a message (a string) using the format() method.
Code:
import pandas as pd
df = pd.read_csv('data.csv')
df
A B C
0 Matt 23 0.98
1 Mark 34 9.33
2 Luke 52 2.54
3 John 67 4.73
The message I want to pass this data into:
message = "{} is {} years old and has a score of {}"
My limited understanding of using the .format() method with the message (string)
message.format()
From what I can tell, I need to have the dataframe as 1 of the arguments for the format() method, butother than that, I'm unsure as to how to code this up.
Help/assistance is greatly appreciated.
Upvotes: 0
Views: 613
Reputation: 2897
You can try this:
import pandas as pd
df = pd.DataFrame([['Matt',23,0.98],['Mark',34,0.43]])
message = "{} is {} years old and has a score of {}"
for i,r in df.iterrows():
print(message.format(*r.to_dict().values()))
Output:
Matt is 23 years old and has a score of 0.98
Mark is 34 years old and has a score of 0.43
Upvotes: 3