You can use the following basic syntax to create a bar chart in pandas that includes only the top 10 most frequently occurring values in a specific column:
import pandas as pd import matplotlib.pyplot as plt #find values with top 10 occurrences in 'my_column' top_10 = (df['my_column'].value_counts()).iloc[:10] #create bar chart to visualize top 10 values top_10.plot(kind='bar')
The following example shows how to use this syntax in practice.
Example: Create Bar Chart in Pandas to Visualize Top 10 Values
Suppose we have the following pandas DataFrame that contains information on the team name and points scored by 500 different basketball players:
import pandas as pd import numpy as np from string import ascii_uppercase import random from random import choice #make this example reproducible random.seed(1) np.random.seed(1) #create DataFrame df = pd.DataFrame({'team': [choice(ascii_uppercase) for _ in range(500)], 'points': np.random.uniform(0, 20, 500)}) #view first five rows of DataFrame print(df.head()) team points 0 E 8.340440 1 S 14.406490 2 Z 0.002287 3 Y 6.046651 4 C 2.935118
We can use the following syntax to create a bar chart that displays the top 10 most frequently occurring values in the team column:
import matplotlib.pyplot as plt #find teams with top 10 occurrences top_10_teams = (df['team'].value_counts()).[:10] #create bar chart of top 10 teams top_10_teams.plot(kind='bar')
The bar chart only contains the names of the top 10 most frequently occurring teams.
The x-axis displays the team name and the y-axis displays the frequency.
Note that we can also customize the plot to make it more aesthetically pleasing:
import matplotlib.pyplot as plt #find teams with top 10 occurrences top_10_teams = (df['team'].value_counts()).[:10] #create bar chart of top 10 teams top_10_teams.plot(kind='bar', edgecolor='black', rot=0) #add axis labels plt.xlabel('Team') plt.ylabel('Frequency')
Note that the edgecolor argument added a black border around each bar and the rot argument rotated the x-axis labels 90 degrees to make them easier to read.
Additional Resources
The following tutorials explain how to perform other common tasks in pandas:
Pandas: How to Create a Stacked Bar Chart
Pandas: How to Annotate Bars in Bar Plot
Pandas: How to Plot Multiple Columns on Bar Chart