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 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 'numepisodes'. There should be one row for each unique director, and 'numepisodes' should contain the number of episodes they directed or codirected. Sort this DataFrame so that the most experienced directors appear at the top.
Hints:
Our solution involved creating a new, empty DataFrame with bpdDataFrame adding columns, and using groupby.
For groupby to give meaningful results, you must use it on a DataFrame with at least two columns.
directorsbyepisode episodesdirectedbyapplyparsenames
alldirectors director for directors in directorsbyepisode for director in directors
directorsdf pdDataFramealldirectors, columnsname
directorcounts directorsdfnamevaluecountsresetindex
directorcounts.columns name 'numepisodes'
directors directorcounts.sortvaluesby'numepisodes', ascendingFalseresetindexdropTrue
directors
directorsbyepisode episodesdirectedbyapplyparsenames
alldirectors director for directors in directorsbyepisode for director in directors
directorsdf pdDataFramealldirectors, columnsname
directorcounts directorsdfnamevaluecountsresetindex
directorcounts.columns name 'numepisodes'
directors directorcounts.sortvaluesby'numepisodes', ascendingFalseresetindexdropTrue
directors
name numepisodes
James Burrows
Kevin S Bright
Gary Halvorson
grader.checkq
q results:
q result:
Trying:
isinstancedirectors bpdDataFrame and directors.shape # Check number of rows and columns
Expecting:
True
Line in q
Failed example:
isinstancedirectors bpdDataFrame and directors.shape # Check number of rows and columns
Expected:
True
Got:
False
Question 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 parsenames that turns a string in the 'directedby column to a list of one or two names. Values in the 'directedby column are either single names, or two names separated by an ampersand. For example,
parsenamesJames Burrows' should return James Burrows' and
parsenamesKevin S Bright & Gary Halvorson' should return Kevin S Bright', 'Gary Halvorson'
Then, apply your function to the 'directedby column from the episodes DataFrame, and store the resulting Series in the variable directorsbyepisode. 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 : List 'combining'ismy 'passion'
Logout
Control
Not Trusted
Python ipykern
Run
Markdown
Validate
LHI
git nbdiff
Out: List 'combining', ismy 'passion'
Question ~~ 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 'numepisodes'. There should be one row for each unique director, and 'numepisodes' should contain the number of episodes they directed or codirected. 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.
everything is fine except for the index, reset the index to name and drop the original index ;
cuz rn it looks like this: name numepisodes
James Burrows
Kevin S Bright
Gary Halvorson
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
