Reading Data
In real work you load data far more often than you type it in by hand. pandas can read CSV, Excel, JSON, and SQL — this lesson focuses on read_csv, the one you'll reach for constantly.
Reading a CSV file
Given a file sales.csv sitting next to your script:
order_id,product,quantity,price 1,Notebook,4,3.50 2,Pen,10,1.20 3,Notebook,2,3.50
import pandas as pd
df = pd.read_csv("sales.csv")
print(df)
order_id product quantity price 0 1 Notebook 4 3.5 1 2 Pen 10 1.2 2 3 Notebook 2 3.5
The first line of the file became the column names automatically, and pandas guessed a sensible dtype for each column — whole numbers for order_id and quantity, decimals for price.
Inspecting a loaded DataFrame
Two methods worth running on reflex right after loading anything real: .info() for a structural summary, and .describe() for quick statistics on the numeric columns:
print(df.info()) print(df.describe())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 order_id 3 non-null int64
1 product 3 non-null object
2 quantity 3 non-null int64
3 price 3 non-null float64
order_id quantity price
count 3.0 3.0 3.0
mean 2.0 5.3 2.7
std 1.0 4.2 1.3
....info() tells you the row count, each column's dtype, and how many non-missing values it has — the fastest way to spot missing data before it causes a problem three steps later. .describe() gives count, mean, standard deviation, min/max, and quartiles for every numeric column at once.
read_csv guesses each column's type from the data it sees, and it's not always right for your purposes. A column of ZIP codes like 07030 gets read as the integer 7030, silently dropping the leading zero — you'd need dtype={"zip": str} to keep it as text.