pandas:How to display ‘0’ by value_counts() when value does not exit

Published on:
Last updated:

This post is also available in: 日本語 (Japanese)

When I try to count value by value_counts(), I can not count the non-existing value.
But, in daya analysis, I want to explicitly get value '0' count the non-existing value by value_counts().
As one of the solutions at that time, I wrote a sample code as a memo that uses the Series.get() function to set the default value to the non-existing value and display it as "0".

Series.get(key, default=None)
- If the key isn’t found, the default value will be used.

If all the value exist that you want to count, the code would be something like the following (when both True/False exist in the Series).

import pandas as pd

df_sample = pd.Series([True,False,True,True])
df_sample = df_sample.value_counts(normalize=True)

print(df_sample)
"""
True     0.75
False    0.25
dtype: float64
"""

However, if the value you want to count does not exist, 'False' cannot be explicitly counted as in the code below (False does not exist in the Series and cannot be counted).

import pandas as pd

df_sample = pd.Series([True,True,True,True])
df_sample = df_sample.value_counts(normalize=True)

print(df_sample)
"""
True    1.0
dtype: float64
"""

So, if the element you want to count does not exist, but you want to explicitly count it as "0", use the Series.get() function to explicitly set the False count to '0'.
This is one way to display the '0' count.
It is also useful for conditional branching in if statements.

import pandas as pd

df_sample = pd.Series([True,True,True,True])
df_sample = df_sample.value_counts(normalize=True)

print(f"True	{df_sample.get(True, 1)}")
print(f"False	{df_sample.get(False, 0)}")
"""
True	1.0
False	0
"""
No tags for this post.

About
Kuniyoshi Takemoto is the founder of Amelt.net LLC, and editor of this blog(www.amelt.net).Learn more and follow me on LinkedIn.