⚡0 XP🔥0

Loading and Exploring Data

Lesson 3 of 7 ⏱ 3 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs Python

Read CSV and Excel files with pandas, parse dates, take a first look with head, info, describe and value_counts, answer quick questions by filtering and sorting, and save results.

Reading files#

▶ Try it Yourselfsales.csvPython
Ctrl + Enter to run

Other readers:

Source Function
CSV pd.read_csv("file.csv")
Excel pd.read_excel("file.xlsx", sheet_name="Sales") (needs openpyxl)
JSON pd.read_json("file.json")
SQL database pd.read_sql("SELECT ...", connection)
Web table pd.read_html(url)
Clipboard pd.read_clipboard() - copy cells in Excel, then run

Useful read_csv options:

pd.read_csv("file.csv",
            parse_dates=["order_date"],   # convert to dates
            usecols=["order_id", "sales"],# read only some columns
            dtype={"pincode": str},       # keep leading zeros
            na_values=["NA", "-", ""],    # treat these as missing
            encoding="utf-8")             # Hindi text etc.
Tip

In Jupyter/Colab, the last line of a cell is displayed automatically - you can just write df.head(). In scripts (and this editor) use print().

The first five commands on any new dataset#

▶ Try it Yourselfsales.csvPython
Ctrl + Enter to run
हिंदी में समझें

किसी भी नए dataset को देखते ही ये 5 commands चलाएँ:

  1. df.head() - शुरुआत की कुछ rows कैसी दिखती हैं
  2. df.info() - कितने columns, किस type के, कहाँ खाली values हैं
  3. df.describe() - numbers का min, max, औसत, quartiles
  4. df.isna().sum() - हर column में कितनी खाली values
  5. df.duplicated().sum() - कितनी duplicate rows

इसे data profiling कहते हैं - इससे 2 मिनट में dataset की पूरी तस्वीर मिल जाती है।

Exploring categories#

▶ Try it Yourselfsales.csvPython
Ctrl + Enter to run

Quick questions with filters#

▶ Try it Yourselfsales.csvPython
Ctrl + Enter to run

The .dt accessor for dates#

Code Gives
df["order_date"].dt.year 2025
.dt.month / .dt.month_name() 10 / October
.dt.day_name() Tuesday
.dt.quarter 4
.dt.to_period("M") 2025-10

The .str accessor for text#

Code Does
df["product"].str.contains("Laptop") True/False mask
.str.upper(), .str.title(), .str.strip() clean text
.str.split("-").str[1] part of a code (VM-2025-00811 → 2025)
.str.len() length

Selecting by data type#

df.select_dtypes("number").columns      # numeric columns
df.select_dtypes("object").columns      # text columns

Saving results#

result.to_csv("big_orders.csv", index=False)
result.to_excel("report.xlsx", sheet_name="Big orders", index=False)   # needs openpyxl

index=False stops pandas from writing the 0, 1, 2... index as an extra column.

Summary#

  • pd.read_csv / read_excel load data; use parse_dates, usecols, dtype, na_values.
  • Profile every dataset: head, info, describe, isna().sum(), duplicated().sum().
  • value_counts, nunique, unique explore categories.
  • Filter with masks, use .dt for dates and .str for text; nlargest for top N.
  • Save with to_csv / to_excel (index=False).

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Quick facts

Load sales.csv into df (parse order_date as a date). Create:

  • n_orders - number of rows
  • total_sales - sum of sales
  • top_city - the city with the most orders (use value_counts)
Dataset: sales.csv (already loaded - just use the file name)
2

Filter and sort

From sales.csv, create big_west - orders from the West region with sales ≥ 50,000, keeping only columns order_id, city, product, sales, sorted by sales (highest first).

Dataset: sales.csv (already loaded - just use the file name)

Quick Quiz

Q1.

Which function reads a CSV file?

Q2.

Which method shows column types and non-null counts?

Q3.

What does df['region'].value_counts() return?

Q4.

How do you make pandas treat order_date as a real date while reading?

Q5.

Which returns the 5 orders with the highest sales?