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

Returning count of 0 if value doesn't exist Pandas DataFrame

I am trying to set 2 variables as counts of a specific value in a column.

I have one variable here:

missing_gmt = missing_records._merge.value_counts().Missing_in_GMTLib

That correctly returns: 44

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

I also have another variable here:

missing_nsl = missing_records._merge.value_counts().Missing_in_NSL

Which should return 0 (no records exist) but instead is throwing:

AttributeError: 'Series' object has no attribute 'Missing_in_NSL'

How can I bypass this error so it returns 0 instead?

>Solution :

Series support .get(), so:

.value_counts().get('Missing_in_NSL', 0)

Docs: Series.get() (though the examples there are for df.get())

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