Question: Question 1 . 2 . Now it's time to count the number of episodes each director directed. Make a DataFrame called directors, indexed by 'name',

Question 1.2. Now it's time to count the number of episodes each director directed. Make a DataFrame called directors, indexed by 'name', with one column called 'num_episodes'. There should be one row for each unique director, and 'num_episodes' should contain the number of episodes they directed or co-directed. Sort this DataFrame so that the most experienced directors appear at the top.
Hints:
Our solution involved creating a new, empty DataFrame with bpd.DataFrame(), adding columns, and using groupby.
For groupby to give meaningful results, you must use it on a DataFrame with at least two columns.
directors_by_episode = episodes['directed_by'].apply(parse_names)
all_directors =[director for directors in directors_by_episode for director in directors]
directors_df = pd.DataFrame(all_directors, columns=['name'])
director_counts = directors_df['name'].value_counts().reset_index()
director_counts.columns =['name', 'num_episodes']
directors = director_counts.sort_values(by='num_episodes', ascending=False).reset_index(drop=True)
directors
directors_by_episode = episodes['directed_by'].apply(parse_names)
all_directors =[director for directors in directors_by_episode for director in directors]
directors_df = pd.DataFrame(all_directors, columns=['name'])
director_counts = directors_df['name'].value_counts().reset_index()
director_counts.columns =['name', 'num_episodes']
directors = director_counts.sort_values(by='num_episodes', ascending=False).reset_index(drop=True)
directors
name num_episodes
0 James Burrows 3
1 Kevin S. Bright 3
2 Gary Halvorson 2
grader.check("q1_2")
q1_2 results:
q1_2-1 result:
Trying:
isinstance(directors, bpd.DataFrame) and directors.shape ==(29,1) # Check number of rows and columns
Expecting:
True
**********************************************************************
Line 1, in q1_20
Failed example:
isinstance(directors, bpd.DataFrame) and directors.shape ==(29,1) # Check number of rows and columns
Expected:
True
Got:
False
Question 1.1. We want to first handle any episodes with two directors so that later we can count the number of episodes each director contributed to. Create a function called parse_names that turns a string in the 'directed_by' column to a list of one or two names. Values in the 'directed_by' column are either single names, or two names separated by an ampersand. For example,
parse_names('James Burrows') should return ['James Burrows'], and
parse_names('Kevin S. Bright & Gary Halvorson') should return ['Kevin S. Bright', 'Gary Halvorson'].
Then, apply your function to the 'directed_by' column from the episodes DataFrame, and store the resulting Series in the variable directors_by_episode. Do not modify the episodes DataFrame.For the next question, you'll need to know something interesting about how lists work in Python: when you sum two lists together, the output is one giant list that contains all the elements in both lists combined. An example is shown below.
In [10]: ['List', 'combining']+['is','my', 'passion']
Logout
Control Pc
Not Trusted
Python 3(ipykern
Run
Markdown
Validate
LHI
-
git nbdiff
Out[10]: ['List', 'combining', 'is','my', 'passion']
Question 1.2. ~~ Now it's time to count the number of episodes each director directed. Make a DataFrame called directors , indexed by ' name' with one column called 'num_episodes'. There should be one row for each unique director, and 'num_episodes' should contain the number of episodes they directed or co-directed. Sort this DataFrame so that the most experienced directors appear at the top.
Hints:
Our solution involved creating a new, empty DataFrame with bpd. DataFrame(), adding columns, and using groupby .
For groupby to give meaningful results, you must use it on a DataFrame with at least two columns.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!