I am trying to turn a list containing dictionaries into a csv table. I am using pandas however I cant find a way to get my desired result. the data is formatted in the following way:
[[{0:"title1"},{1:"title2"}],[{0:"data1"},{1:"data2"}],[{1:"more data2"}]]
- the outer list is supposed to represent the entire table
- each inner list is supposed to represent a column in the table
- and each dictionary is supposed to represent a row in each column, with each key representing the row number
- in addition not every column has the same number of rows, that's why the row numbers are important.
would appreciate it if someone could help! thank you
I tried using pandas data frame but it completely ignored the dictionary keys and turned the lists into rows rather than columns
I would use a comprehension to reshape your table/data into a list to dictionaries where each dictionary represented a row in the table. At that point, a
csv.DictWriter()or apandas.DataFrame().to_csv()will be able to process it into a CSV file.That should show you:
Now we can more easily write out our CSV.
or
Either should produce a file like: