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

Value count of columns in a pandas DataFrame where where string is 'nan'

Let’s say I have the following pd.DataFrame

>>> df = pd.DataFrame({
    'col_1': ['Elon', 'Jeff', 'Warren', 'Mark'],
    'col_2': ['nan', 'Bezos', 'Buffet', 'nan'],
    'col_3': ['nan', 'Amazon', 'Berkshire', 'Meta'],
})

which gets me

    col_1   col_2   col_3
0   Elon    nan     nan
1   Jeff    Bezos   Amazon
2   Warren  Buffet  Berkshire
3   Mark    nan     Meta

All column types are strings. I would like a way to obtain the number of rows per column where the cell value is 'nan'.

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

Where I simply run the following I get always zeros as missing count since it doesnt check for string which contain nan.

>>> df.isna().sum()

col_1    0
col_2    0
col_3    0
dtype: int64

However, what I want is to get

col_1    0
col_2    2
col_3    1

How can I do that?

>Solution :

you have nan as string , you can do :

df.eq("nan").sum()

output :

col_1    0
col_2    2
col_3    1
dtype: int64
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