In [1]:
%matplotlib inline
import pandas as pd
In [2]:
from IPython.core.display import HTML
css = open('style-table.css').read() + open('style-notebook.css').read()
HTML('<style>{}</style>'.format(css))
Out[2]:
In [3]:
titles = pd.DataFrame.from_csv('data/titles.csv', index_col=None)
titles.head()
Out[3]:
In [4]:
cast = pd.DataFrame.from_csv('data/cast.csv', index_col=None)
cast.head()
Out[4]:
In [ ]:
In [5]:
# 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?
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()
Out[5]:
In [6]:
d = c.Superman - c.Batman
print('Superman years:')
print(len(d[d > 0.0]))
In [7]:
# How many years have been "Batman years",
# with more Batman characters than Superman characters?
print('Batman years:')
print(len(d[d < 0.0]))
In [8]:
# Plot the number of actor roles each year
# and the number of actress roles each year
# over the history of film.
c = cast
c = c.groupby(['year', 'type']).size()
c = c.unstack('type')
c.plot()
Out[8]:
In [9]:
# Plot the number of actor roles each year
# and the number of actress roles each year,
# but this time as a kind='area' plot.
c = cast
c = c.groupby(['year', 'type']).size()
c = c.unstack('type')
c.plot(kind='area')
Out[9]:
In [10]:
# Plot the difference between the number of actor roles each year
# and the number of actress roles each year over the history of film.
c = cast
c = c.groupby(['year', 'type']).size()
c = c.unstack('type')
(c.actor - c.actress).plot()
Out[10]:
In [11]:
# Plot the fraction of roles that have been 'actor' roles
# each year in the hitsory of film.
c = cast
c = c.groupby(['year', 'type']).size()
c = c.unstack('type')
(c.actor / (c.actor + c.actress)).plot(ylim=[0,1])
Out[11]:
In [12]:
# Plot the fraction of supporting (n=2) roles
# that have been 'actor' roles
# each year in the history of film.
c = cast
c = c[c.n == 2]
c = c.groupby(['year', 'type']).size()
c = c.unstack('type')
(c.actor / (c.actor + c.actress)).plot(ylim=[0,1])
Out[12]:
In [13]:
# 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.
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])
Out[13]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: