简体   繁体   中英

Export list to csv file using pandas python

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM