Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created April 23, 2020 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codecademydev/3393d0cce2abafa45765384b40fd5b2a to your computer and use it in GitHub Desktop.
Save codecademydev/3393d0cce2abafa45765384b40fd5b2a to your computer and use it in GitHub Desktop.
Codecademy export
import codecademylib3_seaborn
import pandas as pd
import matplotlib.pyplot as plt
# load rankings data here:
df_wood = pd.read_csv('Golden_Ticket_Award_Winners_Wood.csv')
df_steel = pd.read_csv('Golden_Ticket_Award_Winners_Steel.csv')
df_roller = pd.read_csv('roller_coasters.csv')
# wood = df_wood.head(15)
# steel = df_steel.head(15)
# roller = df_roller.head(15)
# write function to plot rankings over time for 1 roller coaster here:
def ranking_vs_year(df, roll_name, park_name):
str_roll_name =str(roll_name)
str_park_name =str(park_name)
new_df = df.loc[(df['Name'] == str_roll_name) & (df['Park'] == str_park_name)]
new_df.plot(kind = 'line', x = 'Year of Rank', y = 'Rank', legend = False).invert_yaxis()
plt.title(str_roll_name + ' ranking over the years')
plt.ylabel('Ranking')
plt.show()
ranking_vs_year(df_wood, 'Boulder Dash', 'Lake Compounce')
plt.clf()
# write function to plot rankings over time for 2 roller coasters here:
def two_ranking_vs_year(df, roll1, roll2, park1, park2):
str_roll1 = str(roll1)
str_roll2 = str(roll2)
str_park1 = str(park1)
str_park2 = str(park2)
new_df1 = df.loc[(df['Name'] == str_roll1) & (df['Park'] == str_park1)]
new_df2 = df.loc[(df['Name'] == str_roll2) & (df['Park'] == str_park2)]
ax = plt.subplot()
new_df1.plot(kind = 'line', x = 'Year of Rank', y = 'Rank', ax = ax).invert_yaxis()
new_df2.plot(kind = 'line', x = 'Year of Rank', y = 'Rank', ax = ax)
plt.legend([str_roll1, str_roll2])
plt.ylabel('Ranking')
plt.title('Evolution of ' + str_roll1 + ' and ' + str_roll2 + ' rakings through the years')
plt.show()
two_ranking_vs_year(df_wood, 'El Toro', 'Boulder Dash', 'Six Flags Great Adventure' , 'Lake Compounce' )
plt.clf()
# write function to plot top n rankings over time here:
def top_n(df, n):
df_new = df_wood[df_wood['Rank'] <= n]
fig = plt.figure()
ax = plt.subplot()
for roll in set(df_new['Name']):
roll_rankings = df_new[df_new['Name'] == roll]
roll_rankings.plot(kind = 'line', x = 'Year of Rank', y = 'Rank', label=roll, ax = ax).invert_yaxis()
plt.show()
top_n(df_wood, 5)
plt.clf()
# load roller coaster data here:
# write function to plot histogram of column values here:
def hist(df, column):
str_column = str(column)
ax = plt.subplot()
df.hist(str_column, ax = ax)
upper = str_column[0].upper()
upper_str_column = upper + str_column[1:]
plt.xlabel(upper_str_column)
plt.title(upper_str_column + ' Histogram')
plt.show()
hist(df_roller, 'length')
plt.clf()
# write function to plot inversions by coaster at a park here:
def inversions(df, park):
new_df = df[df['park'] == park]
fig = plt.figure(figsize = (10, 5))
ax = plt.subplot()
new_df.plot.bar(x = 'name', y = 'num_inversions', legend = False, ax = ax)
plt.ylabel('Number of Inversions')
plt.xlabel('Name')
plt.xticks(rotation = 80)
plt.title('Number of Inversions per Roller Coast')
plt.show()
inversions(df_roller, 'Cedar Point')
plt.clf()
# write function to plot pie chart of operating status here:
def status_pie(df):
df_operating = df[df['status'] == 'status.operating'].count()
df_closed = df[df['status'] == 'status.closed.definitely'].count()
new_df = df_operating.append(df_closed)
plt.pie(new_df['status'], autopct='%d%%', labels = ['Operating', 'Closed'])
plt.axis('equal')
plt.show()
status_pie(df_roller)
plt.clf()
# write function to create scatter plot of any two numeric columns here:
def roller_scatter(df, column1, column2):
fig = plt.figure(figsize=(10,5))
plt.scatter(x = df[column1], y = df[column2])
plt.xlabel(column1)
plt.ylabel(column2)
plt.title(str(column1) +' vs ' + str(column2))
plt.show()
roller_scatter(df_roller, 'speed', 'height')
plt.clf()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment