Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to create a dictionary from a dataframe where the keys are column names and values are the number of values under each column?

I have a data frame whose dimensions are (356027, 163).
I want to create a dictionary from the data frame where i will have 163 keys with values which are the number of entries in them(i.e. number of non null entries)
I tried using the to_dict() operation but couldn’t insert the values as the count of entries under each column

dict1 = data-frame.to_dict('index')
print(dict1)
for i in dict1:
    print(i)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use DataFrame.count with Series.to_dict, loop by dict is not necessary:

out = data-frame.count().to_dict()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading