How can I create a csv from a dictionary

3358 views python

I have a very unique situation here. My dictionary looks something like this.

d = {'0':df1,'1':df2,'2':df3,...,'1000':df1001}

I want to create a seperate csv file for df1, df2, df3 and so on up to df1001 with the file names as '0'.csv,'1'.csv,'2'.csv and so on upto '1000'.csv respectively.

I have tried using the pandas to_csv but that does not help the cause. Can anyone please help me out here.

Thanks in advance.

answered question

Are you getting any errors using to_csv() ?

2 Answers


Calling to_csv should definitely work.

for k, v in d.items():

posted this

I would recommand using the pandas library. The pandas library can easily create a dataframe from a dict. This dataframe can be stored as a CSV file.

import pandas as pd
df = pd.DataFrame.from_dict(dict)

Now you can save the pandas dataframe as a CSV file:

df.to_csv('results.csv', index=False, header=False)

posted this

Have an answer?


Please login first before posting an answer.