Trying to wrap my head around functions.
In the Roller Coaster project https://www.codecademy.com/practice/projects/roller-coaster, question 3. It is asked to ‘Write a function that will plot the ranking of a given roller coaster over time as a line. Your function should take a roller coaster’s name and a ranking DataFrame as arguments.’
The solution code shows the rows were selected based on requirements, which is fine. And this selection was stored in a variable ‘coaster_rankings’ which is fine too.
However I don’t manage to understand why we use “coaster_rankings[‘Year of Rank’]” as x-values to plot the results (or the same question for “coaster_rankings[‘Rank’]” as y-values) and not rankings_df[‘Year of Rank’]?
How does it allow it ?
Does creating a variable where rows are selected allow to still use the DataFrame it is coming from?
The entire code was this:
def plot_coaster_ranking(coaster_name, park_name, rankings_df):
coaster_rankings = rankings_df[(rankings_df[‘Name’] == coaster_name) & (rankings_df[‘Park’] == park_name)]
fig, ax = plt.subplots()
ax.plot(coaster_rankings[‘Year of Rank’], coaster_rankings[‘Rank’])
ax.set_xticks(coaster_rankings[‘Year of Rank’].values)
plot_coaster_ranking(‘El Toro’, ‘Six Flags Great Adventure’, df_wood)
Thanks a lot for your help!