0

I have three lists and I am using printing the output using f-string formatting.

L = []
name = ['a', 'b', 'c']
dates = ['Jan', 'Feb', 'March']
event = ['Marriage', 'Office event', 'Birthday']
for a, b, c in zip(names, dates, event):
  print(f'I met {a} on {b} at this {c}')
  L.append(I met {a} on {b} at this {c})

How I am trying to insert the output to dataframe:

df = pd.DataFrame(L, columns=['string_values'])

print (df)
2
  • Just append the f-string to the list - L.append(f"I met {a} on {b} at this {c}") Commented Aug 18, 2021 at 14:02
  • list comprehension: [f'I met {a} on {b} at this {c}' for a,b,c in zip(names, dates, event)] Commented Aug 18, 2021 at 14:07

1 Answer 1

2

You already got pointers as comments, here is a full answer:

df = pd.DataFrame({'string_values': [f'I met {a} on {b} at this {c}'
                                     for a, b, c in zip(names, dates, event)]
                   })

output:

                         string_values
0      I met a on Jan at this Marriage
1  I met b on Feb at this Office event
2    I met c on March at this Birthday

using your (corrected) original code:

L = []
names = ['a', 'b', 'c']
dates = ['Jan', 'Feb', 'March']
event = ['Marriage', 'Office event', 'Birthday']
for a, b, c in zip(names, dates, event):
    L.append(f'I met {a} on {b} at this {c}')
df = pd.DataFrame(L, columns=['string_values'])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.