
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Calculate the count of column values of a Pandas DataFrame
To calculate the count of column values, use the count() method. At first, import the required Pandas library −
import pandas as pd
Create a DataFrame with two columns −
dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )
Finding count of "Units" column values using the count() function −
print"Count of values of Units column from DataFrame1 = ",dataFrame1['Units'].count()
In the same way, we have calculated the count from the 2nd DataFrame.
Example
Following is the complete code −
import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) print"DataFrame1 ...\n",dataFrame1 # Finding count of values of a specific column print"Count of values of Units column from DataFrame1 = ",dataFrame1['Units'].count() # Create DataFrame2 dataFrame2 = pd.DataFrame( { "Product": ['TV', 'PenDrive', 'HeadPhone', 'EarPhone', 'HDD'], "Price": [8000, 500, 3000, 1500, 3000] } ) print"\nDataFrame2 ...\n",dataFrame2 # Finding count of values of all the column print"\nCount of column values from DataFrame2 = \n",dataFrame2.count()
Output
This will produce the following output −
DataFrame1 ... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Tesla 80 4 Bentley 110 5 Jaguar 90 Count of values of Units column from DataFrame1 = 6 DataFrame2 ... Price Product 0 8000 TV 1 500 PenDrive 2 3000 HeadPhone 3 1500 EarPhone 4 3000 HDD Count of column values from DataFrame2 = Price 5 Product 5 dtype: int64
- Related Articles
- Python - Calculate the mean of column values of a Pandas DataFrame
- Python - Calculate the median of column values of a Pandas DataFrame
- Python - Calculate the maximum of column values of a Pandas DataFrame
- Python - Calculate the minimum of column values of a Pandas DataFrame
- Python – Group and calculate the sum of column values of a Pandas DataFrame
- Python - Calculate the variance of a column in a Pandas DataFrame
- Python - Calculate the standard deviation of a column in a Pandas DataFrame
- How to count the NaN values in a column in a Python Pandas DataFrame?
- Count the frequency of a value in a DataFrame column in Pandas
- Python - Round number of places after the decimal for column values in a Pandas DataFrame
- Python - How to Count the NaN Occurrences in a Column in Pandas Dataframe?
- Python - Reverse the column order of the Pandas DataFrame
- Python - Replace values of a DataFrame with the value of another DataFrame in Pandas
- Python – Center align column headers of a Pandas DataFrame
- How to Groupby values count on the Pandas DataFrame

Advertisements