Coding Ref

What does Count() do in Pandas?

What does Count() do in Pandas?

In pandas, the count() method returns the number of non-null values in each column of a DataFrame.

This is useful for determining the number of valid values in each column, and can help identify any missing or invalid values in the data.

Here is an example of how to use the count() method in pandas:

main.py
import pandas as pd

# create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3, None], 'B': [4, None, 6, 8], 'C': [-3, None, 1, -7]})

# count the number of non-null values in each column
counts = df.count()

# print the resulting Series
print(counts)

This code will output the following Series:

output
A    3
B    3
C    3
dtype: int64

As you can see, the count() method returned the number of non-null values in each column of the DataFrame. In this case, all columns had three non-null values.

You can use the count() method to quickly summarize the data in a DataFrame and identify any columns that may have missing or invalid values.

You'll also like

Related tutorials curated for you

    How to convert Pandas timestamp to datetime

    What does Diff() do in Pandas?

    What is Pandas Cumsum()?

    How to make a crosstab in Pandas

    How to stack two Pandas DataFrames

    How to print a specific row in a Pandas DataFrame?

    How to round in Pandas

    How to calculate the standard deviation in Pandas DataFrame

    Pandas read SQL

    fillna() in Pandas

    How to concatenate in Pandas

    How to use where() in Pandas