practice DataAnalyzeVisu
中文版:数据分析与可视化练习
Data Analysis and Data Visualization
- Import the pandas library in Python and use pd as an alias.
- 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('name.csv')- Count the total number of unique teams in the dataset and list their names.
teams = # hint: a['type_name'].unique()
num_teams = len(teams) # hint: len(a)
team_names = list(teams) # hint: list(a)- 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()
# or a.groupby('type_name').[['type_name1','type_name2','type_name3']].mean().reset_index()
team_stats =
print(team_stats)- Create a bar chart to show the average age of players in each team.
import matplotlib.pyplot as plt
#hint: plt.figure()
#hint: plt.bar(x, y)- Set a title for the chart. (recreate a bar chart first, all plotting operations of one chart must be in the same cell.)
import matplotlib.pyplot as plt
plt.figure()
plt.bar(teams, team_stats['Age'])
# plt.title('...')- Set labels for both axes of the chart.
import matplotlib.pyplot as plt
plt.figure()
plt.bar(teams, team_stats['Age'])
# plt.xlabel('...')
# plt.ylabel('...')- 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)- Group all players by age and display the average, maximum, and minimum salary for each age group.
age_salary_stats = df.groupby(?)[?].agg(?).reset_index()
# Print the resulting DataFrame
print("Average, Max, and Min Salary by Age:")
print(age_salary_stats)- 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(?)
plt.plot(?)
plt.plot(?)
# Set chart title and axis labels
plt.?('Relationship between Age and Salary (Average / Max / Min)')
plt.?('Age')
plt.?('Salary (USD)')
# Add legend
plt.legend()
# Show the chart
plt.show()