AITC Wiki

practice DataAnalyzeVisu solution

数据分析与可视化练习参考答案

practice DataAnalyzeVisu solution

中文版:数据分析与可视化练习参考答案

Data Analysis and Data Visualization

  1. Import the pandas library in Python and use pd as an alias.
  2. Read the nba.csv file using pandas and store it in a DataFrame named df
import pandas as pd #hint: import ... as ...
df = pd.read_csv('nba.csv') # pd.read_csv('name.csv')
  1. Count the total number of unique teams in the dataset and list their names.
teams = df['Team'].unique() # hint: a['type_name'].unique()
num_teams = len(teams) # hint: len(a)
team_names = list(teams) # hint: list(a)
  1. For each team, calculate the average age, average height, and average salary of its players
# hint: a.groupby('type_name').agg({'type_name':'mean'}).reset_index()
# a.groupby('type_name').[['type_name1','type_name2','type_name3']].mean().reset_index()
team_stats = df.groupby('Team')[['Age', 'Height', 'Salary']].mean().reset_index()
print(team_stats)
  1. Create a bar chart to show the average age of players in each team.
import matplotlib.pyplot as plt
 
plt.figure() #plt.figure()
plt.bar(teams, team_stats['Age'])
#plt.bar(x, y)
  1. Set a title for the chart.
import matplotlib.pyplot as plt
 
plt.figure() #plt.figure()
plt.bar(teams, team_stats['Age'])
#plt.bar(x, y)
 
plt.title('Average Age of NBA Teams')
Attention!

Complete all plotting operations within the same cell.

Otherwise:

import matplotlib.pyplot as plt
 
plt.figure() #plt.figure()
plt.bar(teams, team_stats['Age'])
#plt.bar(x, y)
plt.title('Average Age of NBA Teams')
  1. Set labels for both axes of the chart.
import matplotlib.pyplot as plt
 
plt.figure() #plt.figure()
plt.bar(teams, team_stats['Age'])
#plt.bar(x, y)
 
plt.xlabel('Team')
plt.ylabel('Age')
  1. Print the maximum and minimum height of players for each team.
# hint: a.groupby('type_name')['type_name'].agg(['max', 'min'])
height_stats = df.groupby('Team')['Height'].agg(['max', 'min'])
 
# Print the result
print("Maximum and Minimum Height per Team:")
print(height_stats)
  1. Group all players by age and display the average, maximum, and minimum salary for each age group.
age_salary_stats = df.groupby('Age')['Salary'].agg(['mean', 'max', 'min']).reset_index()
 
# Print the resulting DataFrame
print("Average, Max, and Min Salary by Age:")
print(age_salary_stats)
  1. Create a line chart to show the relationship between age and (average,max,min) salary. Be sure to set an appropriate title,axis labels and legend.
# Create the line chart
plt.figure(figsize=(10, 5))
# Plot mean, max, and min salary by age
plt.plot(age_salary_stats['Age'], age_salary_stats['mean'], label='Average Salary', marker='o')
plt.plot(age_salary_stats['Age'], age_salary_stats['max'], label='Max Salary', marker='^')
plt.plot(age_salary_stats['Age'], age_salary_stats['min'], label='Min Salary', marker='s')
 
# Set chart title and axis labels
plt.title('Relationship between Age and Salary (Average / Max / Min)')
plt.xlabel('Age')
plt.ylabel('Salary (USD)')
# Add legend
plt.legend()
 
# Show the chart
plt.show()