0

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.

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.