Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write list of dictionary into CSV Python

Suppose I have the list of dictionary dataset like this,

data_set = [
    {'Active rate': [0.98, 0.97, 0.96]},
    {'Operating Expense': [3.104, 3.102, 3.101]}
]

I need to iterate the list of dictionary and put the keys as column headers and its values as the rows and write it to the CSV file.

Active rate    Operating Expense
0.98           3.104
0.97           3.102
0.96           3.101

This is what I tried

data_set = [
    {'Active rate': [0.98, 0.931588, 0.941192]},
    {'Operating Expense': [3.104, 2.352, 2.304]}
]

import csv

with open('names.csv', 'w') as csvfile:
    fieldnames = ['Active rate', 'Operating Expense']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'Active rate': 0.98, 'Operating Expense': 3.102})
    writer.writerow({'Active rate': 0.97, 'Operating Expense': 3.11})
    writer.writerow({'Active rate': 0.96, 'Operating Expense': 3.109})

For brevity, I have reduced the keys to 2 and list of values to 3.

How to approach this problem?

Thanks

like image 793
PyAn Avatar asked Nov 27 '15 10:11

PyAn


People also ask

How do I write a dictionary list in csv?

To write a dictionary of list to CSV files, the necessary functions are csv. writer(), csv. writerow(). This method writes a single row at a time.

How do I store a dictionary in csv?

Use csv module from Python's standard library. Easiest way is to open a csv file in 'w' mode with the help of open() function and write key value pair in comma separated form. The csv module contains DictWriter method that requires name of csv file to write and a list object containing field names.

How do I convert a list into a CSV file in Python?

To convert a list of lists to csv in python, we can use the csv. writer() method along with the csv. writerow() method.


2 Answers

d1 = {'Active rate': [0.98, 0.931588, 0.941192]}
d2 = {'Operating Expense': [3.104, 2.352, 2.304]}

with open('names.csv', 'w') as csvfile:
    fieldnames = zip(d1, d2)[0]
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()

    for row in zip(d1['Active rate'], d2['Operating Expense']):
        writer.writerow(dict(zip(fieldnames, row)))

For performance, you might want to use itertools.izip over zip depending on the length of lists.

like image 64
Ozgur Vatansever Avatar answered Sep 29 '22 08:09

Ozgur Vatansever


The following approach should work for the data structure you have given:

import csv

data_set = [
    {'Active rate': [0.98, 0.97, 0.96]},
    {'Operating Expense': [3.104, 3.102, 3.101]}
]

fieldnames = ['Active rate', 'Operating Expense']
rows = []

for field in fieldnames:
    for data in data_set:
        try:
            rows.append(data[field])
            break
        except KeyError, e:
            pass

with open('names.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(fieldnames)
    csv_output.writerows(zip(*rows))

Giving you the following CSV output file:

Active rate,Operating Expense
0.98,3.104
0.97,3.102
0.96,3.101
like image 25
Martin Evans Avatar answered Sep 29 '22 07:09

Martin Evans