Just to enable it, writing a dictionary to a CSV file can also be done with the Pandas package. In the above example, it could be something like this:
mydict = {'key1': 'a', 'key2': 'b', 'key3': 'c'}
import pandas as pd (pd.DataFrame.from_dict(data=mydict, orient='index') .to_csv('dict_file.csv', header=False))
The main thing to consider is to set the 'orient' parameter to 'index' inside the from_dict method. This allows you to choose whether you want to write each dictionary key on a new line.
Also, inside the to_csv method, the header parameter is False to only have dictionary elements without annoying strings. You can always set the column and index names inside the to_csv method.
Your output will look like this:
key1,a key2,b key3,c
If instead you want the keys to be column names, just use the default 'orient' parameter, which is 'columns', as you could check in the documentation links.
Ivan Calderon Aug 03 '17 at 0:02 2017-08-03 00:02
source share