Skip to content

Introduction to Python

Run the hidden code cell below to import the data used in this course.


1 hidden cell

Take Notes

Add notes about the concepts you've learned and code cells with code you want to keep.

Add your notes here

# Add your code snippets here

Explore Datasets

Use the arrays imported in the first cell to explore the data and practice your skills!

  • Print out the weight of the first ten baseball players.
  • What is the median weight of all baseball players in the data?
  • Print out the names of all players with a height greater than 80 (heights are in inches).
  • Who is taller on average? Baseball players or soccer players? Keep in mind that baseball heights are stored in inches!
  • The values in soccer_shooting are decimals. Convert them to whole numbers (e.g., 0.98 becomes 98).
  • Do taller players get higher ratings? Calculate the correlation between soccer_ratings and soccer_heights to find out!
  • What is the average rating for attacking players ('A')?
#Print out the weight of the first ten baseball players
import pandas as pd
baseball = pd.read_csv('baseball.csv')
baseball.iloc[0:11, 4]
Hidden output
#What is the median weight of all baseball players in the data?
baseball['Weight'].median()
Hidden output
#Print out the names of all players with a height greater than 80 (heights are in inches).
baseball['Name'][baseball['Height'] == 80]
Hidden output
baseball.shape
Hidden output
#Who is taller on average? Baseball players or soccer players? Keep in mind that baseball heights are stored in inches!
football = pd.read_csv('soccer.csv')
football_avg_h = football['height'].sum() / 8847
baseball_avg_h = baseball['Height'].sum() / 1015

print("Football players are taller on average: ", football_avg_h > baseball_avg_h)
Hidden output
#The values in soccer_shooting are decimals. Convert them to whole numbers (e.g., 0.98 becomes 98).

#football['shooting'] = football['shooting'] * 100

football.head()
Hidden output
#Do taller players get higher ratings? Calculate the correlation between soccer_ratings and soccer_heights to find out!
df = {
    'Ratings' : soccer_ratings,
    'Heights' : soccer_heights
}

data = pd.DataFrame(df)

correlation = data.corr() 
print(correlation)
print('There is no correlation between raiting and height')
Hidden output
#What is the average rating for attacking players ('A')?

football_A = football[football['position'] == 'A']

avg_rating_A = football_A['rating'].sum() / 2082
print(avg_rating_A)
Hidden output