Lab on Importing data in Python

Importing and Inspecting Data with Pandas
PANDAS • PRACTICAL DATA ANALYSIS

Importing and
Inspecting Data

CSV • Text • Excel • JSON

A hands-on lesson for loading files correctly and understanding a dataset before beginning analysis.

Learning outcomes

ChooseMatch the Pandas reader with the source format.
LoadSet the path and necessary parsing options.
InspectCheck sample rows, dimensions, columns, and types.
VerifyConfirm that the DataFrame matches expectations.

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.

Important: The lesson page is read-only. Only the CSV practice dataset is downloadable.

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")
OptionPurpose
sepSpecifies the delimiter, such as comma, tab, semicolon, or pipe.
headerIdentifies the row containing column names.
namesSupplies custom column names.
encodingSpecifies character encoding, such as UTF-8.
na_valuesAdds 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
head()Confirms columns and sample values.
tail()Reveals trailers or incomplete final rows.
info()Shows non-null counts, types, and memory usage.
shapeReturns the number of rows and columns.
columnsLists labels and exposes naming problems.
dtypesFinds numeric, text, and date-type issues.

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

SymptomLikely causeNext action
One giant columnWrong delimiterSet the correct sep.
Garbled charactersWrong encodingSpecify UTF-8 or the source encoding.
Unnamed columnsExtra delimiters or indexReview header and index_col.
Numbers stored as objectSymbols or mixed textClean commas, currency signs, and stray text.
Wrong row countHeaders or footers includedUse skiprows, nrows, or skipfooter.
JSON parse failureJSON Lines or nestingUse lines=True or normalize nested data.

Reusable checklist

01
Identify
Determine the format, delimiter, sheet, or JSON structure.
02
Import
Use the matching Pandas reader.
03
Preview
Run head() and tail().
04
Measure
Review shape and columns.
05
Validate
Run info(), dtypes, and missing-value checks.
06
Correct
Fix parsing options, data types, missing values, and names.
A reliable analysis begins with a verified DataFrame.