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