value_counts() in pandas | value_counts in pandas example on entire dataframe & column
In this pandas tutorial, we will discuss about:
-
value_counts pandas method use in pandas DataFrame,
-
value_counts on column pandas,
-
value_counts in pandas dataframe(when applied on entire dataframe),
-
value_counts in pandas example,
First lets create a dataframe that will be used to demonstrate this method - value_counts pandas dataframe.
DataFrame in pandas is two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.
We create DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe , The data can be a dictionary that stores list of values with specified key
Example: Create pandas dataframe
In this example, we will create dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
#display the dataframe
print(data)
Output: dataframe is created below
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
Now lets apply this value_counts on column pandas and entire dataframe using value_counts() in pandas.
What is value_counts in pandas?
value_counts() in pandas is used to return the count of occurance of each value in a dataframe or in a particular column.
If no column is mentioned, value_counts() in pandas will return the count of each row.
Syntax:
dataframe.value_counts(sort,ascending,dropna)
where, dataframe is the input dataframe.
Parameters:
-
sort - this is used to sort the data that is returned from the dataframe
-
ascending is used to get the data in ascending or descending order.It will take boolean values. If it is True → then the data is sorted in ascending order and if its set to false → the data is sorted in descending order.
-
dropna is used to drop / remove if any null values are present in the dataframe.
Now lets see few value_counts in pandas example.
Example 1: value_counts in pandas example applied on entire dataframe
In this value_counts in pandas example, we are applying value_counts() on entire dataframe.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
#display the dataframe by applying value_counts
print(data.value_counts())
Output: value count pandas dataframe result for above code
college_id college_name college_address Total Staff
c-001 vignan university guntur 1200 1
c-002 RVR - JC guntur 5644 1
c-004 Andhra University guntur 670 1
c-021 vvit guntur 3422 1
dtype: int64
From the above value_counts() in pandas example,
-
we returned the count of each row by applying the value_counts() function on the entire dataframe.
-
As each row appeared unique, so the value count for each row is 1.
Example 2: value_counts in pandas example applied on column
In this value_counts on column pandas example, we will apply value_counts() on particular columns
import pandas as pd
from tabulate import tabulate
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
#display the dataframe by applying value_counts on college_address
print(data['college_address'].value_counts())
#display the dataframe by applying value_counts on college_name
print(data['college_name'].value_counts())
Output: value counts in pandas column result
guntur 4
Name: college_address, dtype: int64
vignan university 1
vvit 1
RVR - JC 1
Andhra University 1
Name: college_name, dtype: int64
From the above value_counts() in pandas,
-
we returned the count of each value from college_address and college_name column.
-
As the value - guntur from college_address column occured 4 times.
-
Hence the value counts for this column is 4 and the values occured unique in college_name column, so the value count is 1 for each value.
Thus we have learned how to apply value_counts on column pandas and entire dataframe by using value_counts in pandas dataframe.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Mar 19,2022