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 aryya
• 7,460 points
1,178 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
27,735 views
–1 vote
2 answers
0 votes
2 answers

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

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
23,843 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
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
3,458 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
6,693 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP