Count Rows in Pandas DataFrame

In this Python Pandas tutorial, we will learn about Count Rows in Python Pandas Dataframe. Also, we will cover these topics.

  • Count Rows in Pandas DataFrame
  • Count Entries in Pandas DataFrame
  • Count Duplicate Rows in Pandas DataFrame
  • Count Distinct rows in Pandas DataFrame
  • Count Unique Rows in Pandas DataFrame
  • Count Rows in a Pandas DataFrame that Satisfies a Condition
  • Count Records in Pandas df
  • Count Rows in Series Pandas

Count Rows in Pandas DataFrame

In this section, we will learn how to count rows in Pandas DataFrame.

  • Using count() method in Python Pandas we can count the rows and columns.
  • Count method requires axis information, axis=1 for column and axis=0 for row.
  • To count the rows in Python Pandas type df.count(axis=1), where df is the dataframe and axis=1 refers to column.
df.count(axis=1)

Implementation on Jupyter Notebook

Read Python Pandas CSV Tutorial

Count Entries in Pandas DataFrame

In this section, we will learn how to count entries in Pandas dataframe in Python.

  • Using count() method in Python Pandas we can count total entries for in each column.
  • Count method accepts axis information, axis=1 is for column and axis=0 is for row.
  • To count the rows in Python Pandas type df.count(axis=1), where df is the dataframe and axis=1 refers to column.
df.count(axis=0)

Implementation on Jupyter Notebook

Read Python Pandas DataFrame Iterrows

Count Duplicate Rows in Pandas DataFrame

In this section, we will learn about count duplicate rows in pandas dataframe.

  • There are multiple ways of counting duplicate rows in Python Pandas but most efficient on is using pivot table.
  • Pivot table accepts index or list of columns and aggregate function.
df.pivot_table(index = ['Brand', 'Model', 'Accel', 'TopSpeed'], aggfunc ='size')

Implementation on Jupyter Notebook

Read Python convert DataFrame to list

Count Distinct rows in Pandas DataFrame

In this section, we will learn how to count distinct rows in pandas dataframe.

  • Distinct rows means rows that are not similar to each other or unique rows.
  • Using value_count() method we can find out the distict rows in pandas dataframe.
  • value_count returns each of distinct value of specified column.
  • In our example, we have implemented it on Model column.
df['Model'].value_counts()

Implementation on Jupyter Notebook

Check out, Pandas Delete Column

Count Unique Rows in Pandas DataFrame

In this section, we will count unique rows in Pandas dataframe in Python.

  • Using nunique() method, we can count unique rows in pandas.
  • by default nunique() shows axis=0 that means rows but it can be changed to axis=1.
  • Here is the syntax:
df.nunique()

Implementation on Jupyter Notebook

Read How to Convert Pandas DataFrame to a Dictionary

Count Rows in a Pandas DataFrame that Satisfies a Condition

In this section, we will learn how to count rows in a pandas dataframe that satisfies a condition.

  • There can be any kind of condition to filter out the data so in our case we’ll consider all those columns whose price is above 5000 Euro.
  • Here is the code to perform above condition.
df[df['PriceEuro'] > 50000].count()

Implementation on Jupyter Notebook

Check out, Python Pandas Drop Rows Example

Count Rows in Series Pandas

In this section, we will learn how to count rows in series pandas in python.

  • Using series.count(), we can count the series in Python Pandas.
  • It is similar as we did with DataFrame. In our example, we have created series and then performed operation on it.

Implementation on Jupyter Notebook

Related Posts:

In this tutorial, we have learned about Count Rows in Python Pandas Dataframe. Also, we have covered these topics.

  • Count Rows in Pandas DataFrame
  • Count Entries in Pandas DataFrame
  • Count Duplicate Rows in Pandas DataFrame
  • Count Distinct rows in Pandas DataFrame
  • Count Unique Rows in Pandas DataFrame
  • Count Rows in a Pandas DataFrame that Satisfies a Condition
  • Count Records in Pandas df
  • Count Rows in Series Pandas