Skip to content
Related Articles

Related Articles

How to extract the value names and counts from value_counts() in Pandas ?
  • Last Updated : 11 Dec, 2020

Prerequisites: 

In this article, we will learn how we can extract the names and values using values_count() from panda. The panda library is equipped with a number of useful functions for ‘value_counts’ is one of them. This function returns the counts of unique items in a pandas data frame.

Syntax: 

<object>.value_count()

Approach:

  • Import Required module.
  • Make the DataFrame
  • Process using value_count()
  • Display data

Example 1: To print all the unique country and the first country name in the list.



tolist() function return a list of the values.

Syntax: Index.tolist() 
Parameters : None
Returns : list

Python3




import pandas as pd
import matplotlib.pyplot as plt
  
# Make example dataframe
df = pd.DataFrame([(1, 'Germany'),
                   (2, 'France'),
                   (3, 'Indonesia'),
                   (4, 'France'),
                   (5, 'France'),
                   (6, 'Germany'),
                   (7, 'UK'),
                   (8, 'India'),
                   (9, 'India'),
                   (10, 'Germany')
                   ],
                  columns=['groupid', 'country'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
  
  
# print all unique country name in the list
su1 = df['country'].value_counts().index.tolist()
print(su1)
  
# print 1st unique country name present in a list
su2 = df['country'].value_counts().index.tolist()[0]
print(su2)

Output:

Example 2: To print all the unique values of the column and the first value of the column.

value_count() counts Unique Occurrences of Values in a Column

Syntax: Index.value_count() 
Parameters: None
Returns: the count of occurrences of each of the unique values in this column.



Python3




import pandas as pd
import matplotlib.pyplot as plt
  
# Make example dataframe
df = pd.DataFrame([(1, 'Germany'),
                   (2, 'France'),
                   (3, 'Indonesia'),
                   (4, 'France'),
                   (5, 'France'),
                   (6, 'Germany'),
                   (7, 'UK'),
                   (8, 'India'),
                   (9, 'India'),
                   (10, 'Germany')
                   ],
                  columns=['groupid', 'country'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
  
  
# print country name and counts
su3 = df['country'].value_counts()
print(su3)
  
# print 1st country count in a list
su4 = df['country'].value_counts()[0]
print(su4)

Output:

Example 3: To print our data using a loop from a list.

Python3




import pandas as pd
import matplotlib.pyplot as plt
  
# Make example dataframe
df = pd.DataFrame([(1, 'Germany'),
                   (2, 'France'),
                   (3, 'Indonesia'),
                   (4, 'France'),
                   (5, 'France'),
                   (6, 'Germany'),
                   (7, 'UK'),
                   (8, 'India'),
                   (9, 'India'),
                   (10, 'Germany')
                   ],
                  columns=['groupid', 'country'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
  
  
# printing names and count using loop.
for idx, name in enumerate(df['country'].value_counts().index.tolist()):
    print('Name :', name)
    print('Counts :', df['country'].value_counts()[idx])

Output:

Example 4: To print our data in the form of Bar graph.

Syntax: matplotlib.pyplot.plot(kind=’ ‘)
Parameters: kind: type of graph, i.e. line, bar.
Returns: This returns a Graph.

Python3




import pandas as pd
import matplotlib.pyplot as plt
  
# Make example dataframe
df = pd.DataFrame([(1, 'Germany'),
                   (2, 'France'),
                   (3, 'Indonesia'),
                   (4, 'France'),
                   (5, 'France'),
                   (6, 'Germany'),
                   (7, 'UK'),
                   (8, 'India'),
                   (9, 'India'),
                   (10, 'Germany')
                   ],
                  columns=['groupid', 'country'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])
  
  
# Display data in a form of Graph
df['country'].value_counts().plot(kind='bar')
plt.show()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :