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.

Opgave

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

How do I create a bar chart using this department count data?

Can you explain how to extract the keys and values from the dictionary for plotting?

What other types of visualizations can I use for this data?

close

bookChallenge: Plot Department Distribution

Stryg for at vise menuen

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.

Opgave

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 desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3
single

single

some-alt