Pandas Exercises
Contents
Pandas Exercises¶
Creating DataFrames and Using Sample Data Sets¶
This is the Jupyter Notebook runnable exercises version of the article, Pandas Practice Questions – Fifty-Two Examples to Make You an Expert.
import pandas as pd
import numpy as np
import seaborn as sb
1. Using NumPy, create a Pandas DataFrame with five rows and three columms:
2. For a Pandas DataFrame created from a NumPy array, what is the default behavior for the labels for the columns? For the rows?
3. Create a second DataFrame as above with five rows and three columns, setting the row labels to the names of any five major US cities and the column labels to the first three months of the year.
4. You recall that the Seaborn package has some data sets built in, but can’t remember how to list and load them. Assuming the functions to do so have “data” in the name, how might you locate them? You can assume a Jupyter Notebook / IPython environment and explain the process, or write the code to do it in Python.
Loading data from CSV¶
5. Zillow home data is available at this URL: https://files.zillowstatic.com/research/public_csvs/zhvi/Metro_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv
Open this file as a DataFrame named df_homes in Pandas.
6. Save the DataFrame, df_homes, to a local CSV file, “zillow_home_data.csv”.
7. Load zillow_home_data.csv back into a new Dataframe, df_homes_2
8. Compare the dimensions of the two DataFrames, df_homes and df_homes_2. Are they equal? If not, how can you fix it?
9. A remote spreadsheet showing how a snapshot of how traffic increased for a hypothetical website is available here: https://github.com/CodeSolid/CodeSolid.github.io/raw/main/booksource/data/AnalyticsSnapshot.xlsx. Load the worksheet page of the spreasheet data labelled “February 2022” as a DataFrame named “feb”. Note: the leftmost column in the spreadsheet is the index column.
10. The “Month to Month Increase” column is a bit hard to understand, so ignore it for now. Given the values for “This Month” and “Last Month”, create a new column, “Percentage Increase”.
Basic Operations on Data¶
11. Using Seaborn, get a dataset about penguins into a dataframe named “df_penguins”. Note that because all of the following questions depend on this example, we’ll provide the solution here so no one gets stuck:
df_penguins = sb.load_dataset('penguins')
12. Write the code to show the the number of rows and columns in df_penguins
13. How might you show the first few rows of df_penguins?
14. How can you return the unique species of penguins from df_penguins? How many unique species are there?
15. What function can we use to drop the rows that have missing data?
16. By default, will this modify df_penguins or will it return a copy?
17. How can we override the default?
18. Create a new DataFrame, df_penguins_full, with the missing data deleted.
19. What is the average bill length of a penguin, in millimeters, in this (df_full) data set?
20. Which of the following is most strongly correlated with bill length? a) Body mass? b) Flipper length? c) Bill depth? Show how you arrived at the answer.
21. How could you show the median flipper length, grouped by species?
22. Which species has the longest flippers?
23. Which two species have the most similar mean weight? Show how you arrived at the answer.
24. How could you sort the rows by bill length?
25. How could you run the same sort in descending order?
26. How could you sort by species first, then by body mass?
Selecting Rows, Columns, and Cells¶
Let’s look at some precious stones now, and leave the poor penguins alone for a while. Let’s look at some precious stones now, and leave the poor penguins alone for a while.
27. Load the Seaborn “diamonds” dataset into a Pandas dataframe named diamonds.
28. Display the columns that are available.
29. If you select a single column from the diamonds DataFrame, what will be the type of the return value?
30. Select the ‘table’ column and show its type
31. Select the first ten rows of the price and carat columns ten rows of the diamonds DataFrame into a variable called subset, and display them.
32. For a given column, show the code to display the datatype of the values in the column?
33. Select the first row of the diamonds DataFrame into a variable called row.
34. What would you expect the data type of the row to be? Display it.
A Pandas series
35. Can you discover the names of the columns using only the row returned in #33? Why or why not?Can you discover the names of the columns using only the row returned in #33? Why or why not?
Yes, because a row series should have the columns as the index (See below):
36. Select the row with the highest priced diamond.
37. Select the row with the lowest priced diamond.
Some Exercises Using Time Series¶
38. Load the taxis dataset into a DataFrame, taxis
.
39. The ‘pickup’ column contains the date and time the customer picked up, but it’s a string. Add a column to the DataFrame, ‘pickup_time’, containing the value in ‘pickup’ as a DateTime.
40. We have a hypothesis that as the day goes on, the tips get higher. We’ll need to wrangle the data a bit before testing this, however. First, now that we have a datetime column, pickup_time, create a subset of it to create a new DataFrame, taxis_one_day. This new DataFrame should have values between ‘2019-03-23 00:06:00’ (inclusive) and ‘2019-03-24 00:00:00’ (exlusive).
41. We now have a range from morning until midnight, but we to take the mean of the numeric columns, grouped at one hour intervals. Save the result as df_means, and display it.
42. Create a simple line plot of the value “distance”.
43. Overall, do riders travel further or less far as the day progresses?
44. Create a new column in taxis_means, tip_in_percent
. The source columns for this should be “fare” and “tip”
45. Create a new column, time_interval, as a range of integer values beginning with zero.
Display the correlations between the following pairs of values:
tip_in_percent and distance.
tip_in_percent and passengers.
tip_in_percent and time_interval.
47. Admittedly, the size of the data set is fairly small given how we’ve subsetted it. But based on the values in #45, which of the three pairs show the strongest correlation.
48. Did our hypothesis that people tip more as the day goes on turn out to be warranted?