Data analysis with pandas · Lesson 3 of 15
Reading a CSV file and a first look
Load a CSV file with pd.read_csv, then get to know the data with head, tail, shape, columns and dtypes before you change anything.
Real data usually arrives as a file. The most common format is CSV (comma-separated values): plain text, one row per line, with commas between the values and the column names on the first line. Every spreadsheet program can save one. The examples here use a small file sales.csv from a café: a date, a product, the quantity sold and the price of one item.
import pandas as pd
df = pd.read_csv("sales.csv")
print(df)date product qty price 0 2026-03-02 coffee 3 2.50 1 2026-03-02 tea 1 2.00 2 2026-03-03 cake 2 3.75 3 2026-03-03 coffee 5 2.50 4 2026-03-04 juice 2 3.00 5 2026-03-04 tea 4 2.00 6 2026-03-05 coffee 2 2.50 7 2026-03-05 cake 1 3.75
pd.read_csv reads the file and uses the first line as column names.
Look before you work
With a big file you do not want to print everything. head() shows the first 5 rows (or as many as you ask for), tail() the last ones. shape and columns tell you the size and the names.
import pandas as pd
df = pd.read_csv("sales.csv")
print(df.head(3))
print(df.tail(2))
print(df.shape)
print(list(df.columns)) date product qty price
0 2026-03-02 coffee 3 2.50
1 2026-03-02 tea 1 2.00
2 2026-03-03 cake 2 3.75
date product qty price
6 2026-03-05 coffee 2 2.50
7 2026-03-05 cake 1 3.75
(8, 4)
['date', 'product', 'qty', 'price']Column types
dtypes shows what type pandas chose for each column. Numbers became int64 and float64. Text, and the dates for now, became object. In Cleaning text and types you will turn the dates into real dates.
import pandas as pd
df = pd.read_csv("sales.csv")
print(df.dtypes)date object product object qty int64 price float64 dtype: object
object, some value in it is not a number: a € sign, a space or a word such as n/a. Check dtypes early; it saves surprises later when a sum fails.A quick summary
describe() gives count, mean, spread and quartiles for every number column. Add .round(2) to keep the output short.
import pandas as pd
df = pd.read_csv("sales.csv")
print(df.describe().round(2))
print("Products:", sorted(df["product"].unique()))qty price count 8.00 8.00 mean 2.50 2.75 std 1.41 0.69 min 1.00 2.00 25% 1.75 2.38 50% 2.00 2.50 75% 3.25 3.19 max 5.00 3.75 Products: ['cake', 'coffee', 'juice', 'tea']
Other separators and saving
Many European files use ; between values because the comma is the decimal mark. Tell pandas with sep=";" and decimal=",". To save a DataFrame, use to_csv; index=False leaves out the row numbers.
import pandas as pd
df = pd.read_csv("prices_eu.csv", sep=";", decimal=",")
print(df)
df.to_csv("copy.csv", index=False)
print(pd.read_csv("copy.csv").shape)item price 0 bread 1.80 1 cheese 6.45 2 olives 3.20 (3, 2)
import pandas as pd
df = pd.read_excel("shop.xlsx", sheet_name="March")
print(df.head())
df.to_excel("shop_copy.xlsx", index=False)To try on your own computer: pd.read_excel reads spreadsheets (it needs the extra package openpyxl), and read_csv also takes a full path.
✎ Exercise
Read marks.csv, print the first 3 rows, then print the number of rows and the average mark rounded to 1 decimal.
Hint: pd.read_csv("marks.csv"), then head(3), len(df) and df["mark"].mean().
import pandas as pd
# your code here
Expected output:
student mark 0 Ana 72 1 Ben 85 2 Chloe 64 Rows: 5 Average: 77.6
Show a solution
import pandas as pd
df = pd.read_csv("marks.csv")
print(df.head(3))
print("Rows:", len(df))
print("Average:", round(float(df["mark"].mean()), 1))student mark 0 Ana 72 1 Ben 85 2 Chloe 64 Rows: 5 Average: 77.6
Check yourself
1. What does df.head() show with no number?
head() shows 5 rows by default; head(3) shows 3.
2. A column of prices shows dtype object. What is the most likely reason?
pandas falls back to object (text) when a value in the column cannot be read as a number.
3. How do you read a file that uses ; between values?
sep= tells read_csv which character separates the values.