Importing and
Inspecting Data
A hands-on lesson for loading files correctly and understanding a dataset before beginning analysis.
Learning outcomes
Download the practice dataset
The file contains 50 sales transactions, one deliberately missing amount, multiple cities, product categories, and payment modes.
Save the file in the same folder as your Python program or Jupyter Notebook.
1. Import CSV and text files
read_csv() reads standard CSV files and other delimited text files through the sep parameter.
import pandas as pd
# Standard CSV
sales = pd.read_csv("sales.csv")
# Tab-separated text file
survey = pd.read_csv("survey.txt", sep="\t")
| Option | Purpose |
|---|---|
sep | Specifies the delimiter, such as comma, tab, semicolon, or pipe. |
header | Identifies the row containing column names. |
names | Supplies custom column names. |
encoding | Specifies character encoding, such as UTF-8. |
na_values | Adds markers that should be interpreted as missing data. |
2. Import Excel worksheets
# First worksheet
df = pd.read_excel("results.xlsx")
# Named worksheet
marks = pd.read_excel("results.xlsx", sheet_name="Semester 2")
# Every worksheet as a dictionary of DataFrames
all_sheets = pd.read_excel("results.xlsx", sheet_name=None)
Use sheet_name to select one worksheet, several worksheets, or the complete workbook.
3. Import JSON datasets
JSON may be record-oriented, column-oriented, index-oriented, nested, or line-delimited. Inspect its structure before choosing options.
# Array of row objects
orders = pd.read_json("orders.json")
# One JSON object per line
events = pd.read_json("events.json", lines=True)
4. Inspect the sales DataFrame
print(sales.head()) # first five rows print(sales.tail()) # last five rows sales.info() # schema and non-null counts print(sales.shape) # (rows, columns) print(sales.columns) # column labels print(sales.dtypes) # data type of every column
5. Run a complete command sequence
import pandas as pd
# Import
sales = pd.read_csv("sales.csv")
# Inspect
print(sales.head())
print(sales.tail())
sales.info()
print("Shape:", sales.shape)
print("Columns:", sales.columns.tolist())
print("Types:\n", sales.dtypes)
# Check missing values
print("Missing values:\n", sales.isna().sum())
# Convert the date column
sales["date"] = pd.to_datetime(sales["date"])
# Calculate missing amount from quantity × unit price
sales["amount"] = sales["amount"].fillna(
sales["quantity"] * sales["unit_price"]
)
# Simple analysis
print(sales.groupby("category")["amount"].sum())
print(sales.groupby("city")["amount"].mean())
Expected checks: 50 rows × 10 columns; one missing value in amount before the fill operation; date initially imports as text and is then converted to datetime.
6. Diagnose common import problems
| Symptom | Likely cause | Next action |
|---|---|---|
| One giant column | Wrong delimiter | Set the correct sep. |
| Garbled characters | Wrong encoding | Specify UTF-8 or the source encoding. |
| Unnamed columns | Extra delimiters or index | Review header and index_col. |
| Numbers stored as object | Symbols or mixed text | Clean commas, currency signs, and stray text. |
| Wrong row count | Headers or footers included | Use skiprows, nrows, or skipfooter. |
| JSON parse failure | JSON Lines or nesting | Use lines=True or normalize nested data. |
Reusable checklist
Determine the format, delimiter, sheet, or JSON structure.
Use the matching Pandas reader.
Run
head() and tail().Review
shape and columns.Run
info(), dtypes, and missing-value checks.Fix parsing options, data types, missing values, and names.