How to extract the value names and counts from value_counts() in Pandas ? - GeeksforGeeks

11 Dec, 2020
Prerequisites: 
panda 
matplotlib
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()
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 : NoneReturns : list
Python3
import pandas as pdimport matplotlib.pyplot as plt  # Make example dataframedf = 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 listsu1 = df['country'].value_counts().index.tolist()print(su1)  # print 1st unique country name present in a listsu2 = 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: NoneReturns: the count of occurrences of each of the unique values in this column.
Python3
import pandas as pdimport matplotlib.pyplot as plt  # Make example dataframedf = 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 countssu3 = df['country'].value_counts()print(su3)  # print 1st country count in a listsu4 = df['country'].value_counts()[0]print(su4)
Output:
Example 3: To print our data using a loop from a list.
Python3
import pandas as pdimport matplotlib.pyplot as plt  # Make example dataframedf = 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 pdimport matplotlib.pyplot as plt  # Make example dataframedf = 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 Graphdf['country'].value_counts().plot(kind='bar')plt.show()
Output:
Python Pandas-exercise
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python String | replace()
Create a Pandas DataFrame from Lists
Reading and Writing to text files in Python
*args and **kwargs in Python
How to drop one or multiple columns in Pandas Dataframe
Selecting rows in pandas DataFrame based on conditions