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

Show how much there is of a certain value in each column in pandas

My pandas data frame contains several columns, some of them have missing values which show up as a ? sign. I want to run a for loop to print how much ? there is in each columns of the data. I’m doing something like this:

colnames = ['col_1','col_2','col_3']

for i in colnames:
    print(f'In the {i} feature, the value - ? - occurs {data.i.value_counts()["?"]} times')

The error I get is :

AttributeError: 'DataFrame' object has no attribute 'i'

So I think that problem is with this part – data.i.value_counts(), I tried data[i].value_counts() but that didn’t work eaither..

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

>Solution :

For count values avoid value_counts, because failed selecting ? if value not exist in column. Simplier is compare values by ? and count Trues by sum:

for i in colnames:
    print(f'In the {i} feature, the value - ? - occurs {data[i].eq("?").sum()} times')
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