How to use bins In Python Pandas

If we want to get the count in a particular range of values, then the bins parameter is applied. We can specify the number of ranges(bins).

Syntax:

(data['column_name'].value_counts(bins)

where,

  • data is the input dataframe
  • column_name is the column to get bins
  • bins is the total number of bins to be specified

Example: Get count in particular range of values

Python3




# import pandas module
import pandas as pd
 
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
    'name': ['sravan', 'ojsawi', 'bobby''rohith',
             'gnanesh', 'sravan', 'sravan', 'ojaswi'],
    'subjects': ['java', 'php', 'java', 'php', 'java',
                 'html/css', 'python', 'R'],
    'marks': [98, 90, 78, 91, 87, 78, 89, 90],
    'age': [11, 23, 23, 21, 21, 21, 23, 21]
})
 
# get count of  age column with  6 bins
print(data['age'].value_counts(bins=6))
 
# get count of  age column with  4 bins
print(data['age'].value_counts(bins=4))


Output:

(19.0, 21.0]      4
(21.0, 23.0] 3
(10.987, 13.0] 1
(17.0, 19.0] 0
(15.0, 17.0] 0
(13.0, 15.0] 0
Name: age, dtype: int64
(20.0, 23.0] 7
(10.987, 14.0] 1
(17.0, 20.0] 0
(14.0, 17.0] 0
Name: age, dtype: int64

How to Count Occurrences of Specific Value in Pandas Column?

In this article, we will discuss how to count occurrences of a specific column value in the pandas column.

Dataset in use:

We can count by using the value_counts() method. This function is used to count the values present in the entire dataframe and also count values in a particular column.

Syntax: data[‘column_name’].value_counts()[value]

where

  • data is the input dataframe
  • value is the string/integer value present in the column to be counted
  • column_name is the column in the dataframe

Example: To count occurrences of a specific value

Python3




# import pandas module
import pandas as pd
 
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
    'name': ['sravan', 'ojsawi', 'bobby''rohith',
             'gnanesh', 'sravan', 'sravan', 'ojaswi'],
    'subjects': ['java', 'php', 'java', 'php', 'java',
                 'html/css', 'python', 'R'],
    'marks': [98, 90, 78, 91, 87, 78, 89, 90],
    'age': [11, 23, 23, 21, 21, 21, 23, 21]
})
 
# count values in name column
print(data['name'].value_counts()['sravan'])
 
# count values in subjects column
print(data['subjects'].value_counts()['php'])
 
# count values in marks column
print(data['marks'].value_counts()[89])


Output:

3
2
1

If we want to count all values in a particular column, then we do not need to mention the value.

Syntax:

data['column_name'].value_counts()

Example: To count the occurrence of a value in a particular column 

Python3




# import pandas module
import pandas as pd
 
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
    'name': ['sravan', 'ojsawi', 'bobby''rohith',
             'gnanesh', 'sravan', 'sravan', 'ojaswi'],
    'subjects': ['java', 'php', 'java', 'php', 'java',
                 'html/css', 'python', 'R'],
    'marks': [98, 90, 78, 91, 87, 78, 89, 90],
    'age': [11, 23, 23, 21, 21, 21, 23, 21]
})
 
# count all values in name column
print(data['name'].value_counts())
 
# count all values in subjects column
print(data['subjects'].value_counts())
 
# count all values in marks column
print(data['marks'].value_counts())
 
# count all values in age column
print(data['age'].value_counts())


Output:

If we want to get the results in order (like ascending and descending order), we have to specify the parameter

Syntax:

Ascending order:

data[‘column_name’].value_counts(ascending=True)

Descending Order:

data[‘column_name’].value_counts(ascending=False)

Example: To get results in an ordered fashion 

Python3




# import pandas module
import pandas as pd
 
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
    'name': ['sravan', 'ojsawi', 'bobby''rohith',
             'gnanesh', 'sravan', 'sravan', 'ojaswi'],
    'subjects': ['java', 'php', 'java', 'php', 'java',
                 'html/css', 'python', 'R'],
    'marks': [98, 90, 78, 91, 87, 78, 89, 90],
    'age': [11, 23, 23, 21, 21, 21, 23, 21]
})
 
# count all values in name column in ascending order
print(data['name'].value_counts(ascending=True))
 
# count all values in subjects column in ascending order
print(data['subjects'].value_counts(ascending=True))
 
# count all values in marks column in descending order
print(data['marks'].value_counts(ascending=False))
 
# count all values in age column in descending order
print(data['age'].value_counts(ascending=False))


Output:

Similar Reads

Dealing with missing values

...

Count values with relative frequencies

...

Get details

...

Using size() with groupby()

Here we can count the occurrence with or without NA values. By using dropna parameter to include NA values if set to True, it will not count NA if set to False....

Using count() with groupby()

...

Using bins

We are going to add normalize parameter to get the relative frequencies of the repeated data. It is set to True....

Using apply()

...

Contact Us