Welcome to the Learn Pandas track. These hands-on exercises are targeted for someone who has worked with Pandas a little before.
Each page has a list of relevant resources
you can use if you get stumped. The top item in each list has been custom-made to help you with the exercises on that page.
The first step in most data analytics projects is reading the data file. In this section, you'll create Series
and DataFrame
objects, both by hand and by reading data files.
Run the code cell below to load libraries you will need (including coad to check your answers).
import pandas as pd
pd.set_option('max_rows', 5)
from learntools.advanced_pandas.creating_reading_writing import *
You can check your answers in each of the exercises that follow using the check_qN
function provided in the code cell above (replacing N
with the number of the exercise). For example here's how you would check an incorrect answer to exercise 1:
check_q1(pd.DataFrame())
For the questions that follow, if you use check_qN
on your answer, and your answer is right, a simple True
value will be returned.
If you get stuck, you may run the print(answer_qN())
function to print the answer outright.
Exercise 1: Create a DataFrame
that looks like this:
pd.DataFrame({'Apples': [30], 'Bananas': [21]})
print(check_q1(pd.DataFrame({'Apples': [30], 'Bananas': [21]})))
print('-------')
print(answer_q1())
Exercise 2: Create the following DataFrame
:
pd.DataFrame({'Apples': [35, 41],
'Bananas': [21, 34]},
index=['2017 Sales', '2018 Sales'])
print(check_q2(pd.DataFrame({'Apples': [35, 41],
'Bananas': [21, 34]},
index=['2017 Sales', '2018 Sales'])))
print('-------')
print(answer_q2())
Exercise 3: Create a Series
that looks like this:
Flour 4 cups
Milk 1 cup
Eggs 2 large
Spam 1 can
Name: Dinner, dtype: object
pd.Series(['4 cups', '1 cup', '2 large', '1 can'], index=['Flour', 'Milk', 'Eggs', 'Spam'], name='Dinner')
print(check_q3(pd.Series(['4 cups', '1 cup', '2 large', '1 can'], index=['Flour', 'Milk', 'Eggs', 'Spam'], name='Dinner')))
print('-------')
print(answer_q3())
Exercise 4: Read the following csv
dataset on wine reviews into the a DataFrame
:
The filepath to the CSV file is ../input/wine-reviews/winemag-data_first150k.csv
.
pd.read_csv("../input/wine-reviews/winemag-data_first150k.csv", index_col=0)
print(check_q4(pd.read_csv("../input/wine-reviews/winemag-data_first150k.csv", index_col=0)))
print('-------')
print(answer_q4())
Exercise 6**: Suppose we have the following DataFrame
:
q6_df = pd.DataFrame({'Cows': [12, 20], 'Goats': [22, 19]}, index=['Year 1', 'Year 2'])
Save this DataFrame
to disc as a csv
file with the name cows_and_goats.csv
.
q6_df.to_csv("cows_and_goats.csv")
print(check_q6(q6_df.to_csv("cows_and_goats.csv")))
print('-------')
print(answer_q6())
Exercise 7: This exercise is optional. Read the following SQL
data into a DataFrame
:
The filepath is ../input/pitchfork-data/database.sqlite
. Hint: use the sqlite3
library. The name of the table is artists
.
import sqlite3
conn = sqlite3.connect("../input/pitchfork-data/database.sqlite")
pd.read_sql_query("SELECT * FROM artists", conn)
print(check_q7(pd.read_sql_query("SELECT * FROM artists", conn)))
print('-------')
print(answer_q7())
Move on to the indexing, selecting and assigning workbook
This is part of the Learn Pandas series.