Pandas DataFrame count() method with examples

Spread the love

The count() method in pandas is used to count the number of non-Null values in a row or column.

Syntax –

dataframe.count()

Examples –

Let’s read a dataset to work with.

import pandas as pd

url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/fruit_prices.csv'
df = pd.read_csv(url)
df

1 . Count Number of Non-Null Values in a Column –

By default, the count method counts the number of non-null values in a column. The axis parameter is set to axis=0 or index.

# number of non-null values in a column
df.count()

2 . Count Number of Non-Null Values in a Row –

To count the number of non-null values in a row, we need to set the axis parameter to axis=1 or columns.

# number of non-null values in a row
df.count(axis=1)

Rating: 1 out of 5.

Leave a Reply