c = cast
c = c[(c.character == 'Superman') | (c.character == 'Batman')]
c = c.groupby(['year', 'character']).size()
c = c.unstack()
c = c.fillna(0)
c.head()

d = c.Superman - c.Batman
print('Superman years:')
print(len(d[d > 0.0]))

"""
Explanation: Define a year as a "Superman year" whose films feature more Superman characters than Batman. How many years in film history have been Superman years?
End of explanation
"""


d = c.Superman - c.Batman
print('Batman years:')
print(len(d[d < 0.0]))

"""
Explanation: How many years have been "Batman years", with more Batman characters than Superman characters?
End of explanation
"""


c = cast
#c = c[(c.character == 'Superman') | (c.character == 'Batman')]
c = c.groupby(['year', 'type']).size()
c = c.unstack()
c = c.fillna(0)
c.plot()

"""
Explanation: Plot the number of actor roles each year and the number of actress roles each year over the history of film.
End of explanation
"""


c.plot(kind='area')

"""
Explanation: Plot the number of actor roles each year and the number of actress roles each year, but this time as a kind='area' plot.
End of explanation
"""


c = cast
c = c.groupby(['year', 'type']).size()
c = c.unstack('type')
(c.actor - c.actress).plot()

"""
Explanation: Plot the difference between the number of actor roles each year and the number of actress roles each year over the history of film.
End of explanation
"""


(c.actor/ (c.actor + c.actress)).plot(ylim=[0,1])

"""
Explanation: Plot the fraction of roles that have been 'actor' roles each year in the hitsory of film.
End of explanation
"""


c = cast[(cast["n"] == 2) ]
c = c.groupby(['year','type']).size()
c = c.unstack('type')
(c.actor/ (c.actor + c.actress)).plot(ylim=[0,1])

"""
Explanation: Plot the fraction of supporting (n=2) roles that have been 'actor' roles each year in the history of film.
End of explanation
"""



c = cast
c = c[c.n <= 3]
c = c.groupby(['year', 'type', 'n']).size()
c = c.unstack('type')
r = c.actor / (c.actor + c.actress)
r = r.unstack('n')
r.plot(ylim=[0,1])

"""
Explanation: Build a plot with a line for each rank n=1 through n=3, where the line shows what fraction of that rank's roles were 'actor' roles for each year in the history of film.
End of explanation
"""


