pandas · Lesson 0.1

Your first DataFrame

Load a CSV file into pandas, look at it, and answer your first questions about it.

Practice data: Amazon files

Made up for teaching, not real Amazon data. Read a file with pd.read_csv("customers.csv"). Disclaimer

customers.csv (329 rows): customer_id, first_name, last_name, email, signup_date, country, city, address, is_prime

order_items.csv (2,677 rows): order_id, product_id, quantity, unit_price, discount

orders.csv (1,500 rows): order_id, customer_id, order_ts, status, promo_code, total_amount, shipping_fee

products.csv (96 rows): product_id, product_name, category, brand, price, launch_date

returns.csv (82 rows): return_id, order_id, product_id, return_date, reason

web_events.csv (4,175 rows): event_id, customer_id, event_ts, event_type, device, product_id

pandas is the Python library for working with tables of data. Every code box here runs real pandas in your browser, with a few files of simulated online-shop data ready to read.

Practices ask you to store your answer in a variable called result, and the check compares it with the answer.

1Load a file

pd.read_csv("file.csv") reads a file into a DataFrame, a table with named columns. .head() shows the first five rows.

import pandas as pd

orders = pd.read_csv("orders.csv")
orders.head()
Amazon data

Practice

Read customers.csv and store its first 3 rows in result.

import pandas as pd

customers = pd.read_csv("customers.csv")
result = ...
Amazon data

2How big is it?

.shape gives (rows, columns), and len(df) gives just the number of rows. Check the size first, every time you load data.

import pandas as pd

orders = pd.read_csv("orders.csv")
print(orders.shape)
print(len(orders))
print(list(orders.columns))
Amazon data

Practice

Store the number of rows in products.csv in result.

import pandas as pd

products = pd.read_csv("products.csv")
result = ...
Amazon data

3One column

df["column"] picks one column. Methods like .sum(), .mean() and .max() work on the whole column at once.

import pandas as pd

orders = pd.read_csv("orders.csv")
print(orders["total_amount"].sum())
print(orders["total_amount"].max())
Amazon data

Practice

Store the average total_amount of all orders in result, rounded to 2 decimals.

import pandas as pd

orders = pd.read_csv("orders.csv")
result = ...
Amazon data

4A quick summary

.describe() summarises every number column at once: count, mean, spread and quartiles. It's a fast first look for odd values.

import pandas as pd

orders = pd.read_csv("orders.csv")
orders.describe()
Amazon data

Practice

How many different statuses do orders have? Store the number in result.

import pandas as pd

orders = pd.read_csv("orders.csv")
result = ...
Amazon data

Tip

SQL equivalent: SELECT * FROM orders LIMIT 5 is orders.head(), and SELECT COUNT(*) FROM orders is len(orders).