1

I am trying to export a list that is like that

    [{'Header1': 'Ahmed', 'Notes': 11.96},
 {'Header1': 'Salah', 'Notes': 16.13},
 {'Header1': 'Reda', 'Notes': 20.83}]

I could use csv module like that

with open('Output.csv', 'w', encoding='utf8', newline='') as f:
fc = csv.DictWriter(f, fieldnames=results[0].keys(),)
fc.writeheader()
fc.writerows(results)

and that worked fine How can I use pandas for that purpose?

1 Answer 1

1

Create DataFrame by constructor and then use DataFrame.to_csv:

L =   [{'Header1': 'Ahmed', 'Notes': 11.96},
       {'Header1': 'Salah', 'Notes': 16.13},
       {'Header1': 'Reda', 'Notes': 20.83}]

 df = pd.DataFrame(L)

 df.to_csv('Output.csv', index=False)
Sign up to request clarification or add additional context in comments.

3 Comments

Amazing. I expect you to reply to my question. Thank you very much.
Which is better using pandas or csv module?
@YasserKhalil - It is really complex question, it depends. If need only write data and no processing, I choose csv module. If need processing data I choose pandas.

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.