How can I create a bar chart in Pandas to visualize the top 10 values? 2

How can I create a bar chart in Pandas to visualize the top 10 values?

To create a bar chart in Pandas to visualize the top 10 values, you can use the “plot.bar()” function. First, sort the data in descending order to obtain the top 10 values. Then, specify the column containing the values to be plotted and use the “head(10)” function to select only the top 10 values. Finally, use the “plot.bar()” function to create a bar chart with the selected data. This will allow you to easily visualize and compare the top 10 values in your dataset.

Pandas: Create Bar Chart to Visualize Top 10 Values


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.pyplotas 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.pyplotas 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.pyplotas 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 labelsplt.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.

The following tutorials explain how to perform other common tasks in pandas:

Cite this article

stats writer (2024). How can I create a bar chart in Pandas to visualize the top 10 values?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-bar-chart-in-pandas-to-visualize-the-top-10-values/

stats writer. "How can I create a bar chart in Pandas to visualize the top 10 values?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-bar-chart-in-pandas-to-visualize-the-top-10-values/.

stats writer. "How can I create a bar chart in Pandas to visualize the top 10 values?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-bar-chart-in-pandas-to-visualize-the-top-10-values/.

stats writer (2024) 'How can I create a bar chart in Pandas to visualize the top 10 values?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-bar-chart-in-pandas-to-visualize-the-top-10-values/.

[1] stats writer, "How can I create a bar chart in Pandas to visualize the top 10 values?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I create a bar chart in Pandas to visualize the top 10 values?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top