How can I write nested dictionaries to a csv file?

0 votes
count = {'lt60': {'a': 0, 'b': 0, 'c': 0, 'd': 0}, 'ge60le90': {'a': 4, 'b': 0, 'C': 0, 'd': 0}, 'gt90': {'a': 0, 'b': 1, 'c': 2, 'd': 1} }

What i want to do is, pick the keys from  It60, ge60le90, gt90 and want to write them in a row.
Apr 17, 2018 in Python by ariaholic
• 7,340 points
128 views

1 answer to this question.

0 votes
You can use the pandas library to achieve what you want

[code]
import pandas as pd

count = {'lt60': {'a': 0, 'b': 0, 'c': 0, 'd': 0},
         'ge60le90': {'a': 4, 'b': 0, 'c': 0, 'd': 0},
         'gt90': {'a': 0, 'b': 1, 'c': 2, 'd': 1} }

df = pd.DataFrame(count).rename_axis('relation_type').reset_index()

df = df.rename(columns={'ge60le90': 'confidence<90',
                        'gt90': 'confidence>90',
                        'lt60': 'confidence<60'})

df.to_csv('out.csv', index=False)

#   relation_type  confidence<90  confidence>90  confidence<60
# 0             a              4              0              0
# 1             b              0              1              0
# 2             c              0              2              0
# 3             d              0              1              0
[/code]
answered Apr 17, 2018 by anonymous

Related Questions In Python

+1 vote
4 answers

How to write nested dictionaries to a CSV file

Using DictWriter there is no need in ...READ MORE

answered Oct 18, 2018 in Python by Richard William
4,831 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How can I write a program to add two numbers using functions in python?

def add(a,b):      return a + b #when i call ...READ MORE

answered May 20 in Python by Mohammad
• 3,060 points
75 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 228 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
674 views
0 votes
1 answer

How can I reformat value_counts() analysis in Pandas for large number of columns?

If I were you, I'd do it ...READ MORE

answered Apr 17, 2018 in Python by anonymous
2,071 views