'Distinguish NaN value and string 'NaN' in Pandas value_counts function

in python I have a list of [None, None, 'NaN', 'NaN', None]

When i apply value_counts function on the pandas series of this list, the result is:

NaN 3
NaN 2

which I cannot distinguish which is which.

Is there another method I can distinguish the string 'NaN' literally and value NaN?



Solution 1:[1]

How you visualize the output and what you exactly expect is unclear, but you could add a suffix to the string NaN:

(pd.Series([None, None, 'NaN', 'NaN', None])
   .add(' (str)')
   .value_counts(dropna=False)
)

output:

NaN          3
NaN (str)    2
dtype: int64

Or fillna with a custom string (which makes it work in case you have other types):

(pd.Series([None, None, 'NaN', 'NaN', None])
   .fillna('literal NA')
   .value_counts(dropna=False)
)

output:

literal NA    3
NaN           2
dtype: int64

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1