Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Plot Department Distribution | Analyzing Employee Data
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for HR Specialists

bookChallenge: Plot Department Distribution

Recalling what you have learned about working with lists and dictionaries, you know that counting items in a list can be efficiently achieved using a dictionary, where each unique item serves as a key and its value is the count of occurrences. This is particularly useful in HR scenarios, such as determining how many employees belong to each department. You have also previously explored the basics of using matplotlib to create bar charts, which is a powerful way to visualize categorical data like department distributions.

123456789101112
departments = ["HR", "IT", "Finance", "IT", "HR", "Marketing", "Finance", "IT"] # Count occurrences using a dictionary department_counts = {} for dept in departments: if dept in department_counts: department_counts[dept] += 1 else: department_counts[dept] = 1 print(department_counts) # Output: {'HR': 2, 'IT': 3, 'Finance': 2, 'Marketing': 1}
copy

Once you have the counts for each department stored in a dictionary, you can pass the department names and their corresponding counts to matplotlib's bar chart function. The keys of the dictionary will serve as the labels for each bar, and the values will determine the height of each bar, making it easy to visualize the employee distribution across departments.

Oppgave

Swipe to start coding

Write a function that takes a list of department names and visualizes the number of employees in each department as a bar chart.

  • Count the number of occurrences for each unique department in the list.
  • Use the department names as labels on the x-axis and their counts as the heights of the bars.
  • Display the bar chart with appropriate axis labels and a title.

Løsning

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

close

bookChallenge: Plot Department Distribution

Sveip for å vise menyen

Recalling what you have learned about working with lists and dictionaries, you know that counting items in a list can be efficiently achieved using a dictionary, where each unique item serves as a key and its value is the count of occurrences. This is particularly useful in HR scenarios, such as determining how many employees belong to each department. You have also previously explored the basics of using matplotlib to create bar charts, which is a powerful way to visualize categorical data like department distributions.

123456789101112
departments = ["HR", "IT", "Finance", "IT", "HR", "Marketing", "Finance", "IT"] # Count occurrences using a dictionary department_counts = {} for dept in departments: if dept in department_counts: department_counts[dept] += 1 else: department_counts[dept] = 1 print(department_counts) # Output: {'HR': 2, 'IT': 3, 'Finance': 2, 'Marketing': 1}
copy

Once you have the counts for each department stored in a dictionary, you can pass the department names and their corresponding counts to matplotlib's bar chart function. The keys of the dictionary will serve as the labels for each bar, and the values will determine the height of each bar, making it easy to visualize the employee distribution across departments.

Oppgave

Swipe to start coding

Write a function that takes a list of department names and visualizes the number of employees in each department as a bar chart.

  • Count the number of occurrences for each unique department in the list.
  • Use the department names as labels on the x-axis and their counts as the heights of the bars.
  • Display the bar chart with appropriate axis labels and a title.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
single

single

some-alt