Practice

See how well you have learned Pandas DataFrames by working on the practice problems below:

Practice #1a: Favorites

Import the Pandas library and create a new DataFrame called "df". The new DataFrame should have three columns: Names, Favorite Food, and Favorite Drink. Add three records (people) to the DataFrame.

Practice #1b: Add Column from List

Add a new column called Age to the previous DataFrame. Do this by creating a new list with the new age values and adding that list to the DataFrame under the name "Age". Then create a new DataFrame that only includes the Names column and the Age column by filtering out the other columns.

Practice #1c: Add Column and Update from Loop

Next, we want to examine the age of each person in the DataFrame to see if they were born before the year 2000. For simplicity, assume that anyone age 20 or under was born in 2000 or later and anyone older than 20 was born before 2000. We also want this to work regardless of the number of people in the DataFrame. In other words, we need to loop through each record in the DataFrame one-at-a-time. We will then create a new column to state whether or not each person was born before the year 2000 (e.g., just put True or False in this column for each row).

Practice #2a: Create Movie Streaming DataFrame

You are creating a business to compete with Netflix, and you just acquired your first five movies. Create a DataFrame to store information about your movie collection: Movie Name, Rating, Run Time, and the mainGenre(s). Use the dataset below:

        [['Remember the Titans','PG',113, 'Sport'], ['Forrest Gump', 'PG-13',142,'Drama'], ['Inception','PG-13',148,'Sci-Fi'], ['The Proposal','PG-13',108,'Romance'], ['Dumb & Dumber','PG-13',108,'Comedy']]
        
Practice #2b: Add

Your customers are demanding a way to sort your movie collection by year released. Add a column named "Year" using the data below:

        # Set the year for each of the movies
        # Remember the Titans: 2005
        # Forrest Gump: 1994
        # Inception: 2010
        # The Proposal: 2009
        # Dumb & Dumber: 1994 
        
Practice #2c: Delete

Warner Bros. Pictures just released a statement pulling their movies off of streaming platforms. Remove Inception from the movie library.

Practice #2d: Read

As you create access to your service, customers need to know what genres are available. Print a list of the main genre for each movie.

Practice #2e: Filter

You have an option to merge your movie network with another company. The problem is that company only supports movies shorter than two hours. Make a list of all the movies longer than two hours that will need to be removed.

Practice #2f: Sort

To help make your movies available in the best order for the customers, sort by runTime. If two rows have the same runTime, then sort those by the year released.