```html
``` Skip to contentUntil now, you have created Pandas Series and DataFrames directly inside Python. This is useful for learning, testing, and small examples, but professional Data Analytics rarely begins with manually typing every record into a Python program.
In a real project, data normally comes from external sources such as CSV files, Excel workbooks, databases, APIs, JSON files, or other business systems.
This makes loading data into Pandas one of the most important practical skills for a Data Analyst.
Imagine receiving a customer-sales file containing 100,000 transactions. You would not manually create a DataFrame containing 100,000 rows. Instead, you would import the file into Pandas and then inspect, clean, transform, and analyze it.
The general workflow looks like this:
Data Source
↓
Load into Pandas
↓
Inspect
↓
Validate
↓
Clean
↓
Transform
↓
Analyze
↓
Visualize / Report
Understanding this workflow helps you see where Pandas fits into the larger Data Analytics process.
CSV, which stands for Comma-Separated Values, is one of the most common file formats used for tabular data.
A simple CSV file might look like this:
Name,Age,Course,Marks
Aman,21,Python,82
Priya,22,Data Analytics,91
Rahul,20,SQL,76
Neha,23,Python,88
Pandas provides the read_csv() function for loading CSV files.
import pandas as pd
df = pd.read_csv("students.csv")
print(df)
Once the file is loaded, df becomes a Pandas DataFrame.
You can immediately inspect it:
print(df.head())
This simple command is one of the most common patterns in Pandas:
pd.read_csv()
loads the data, while:
df.head()
lets you quickly inspect the first records.
CSV files are popular because they are simple, portable, and supported by many applications.
They can be created from spreadsheet software, databases, business applications, and other analytical tools.
For example, a company might export:
as CSV files for analysis.
However, CSV files can contain problems such as missing values, inconsistent formatting, incorrect data types, unusual delimiters, encoding issues, and duplicate records.
Therefore, successfully loading the file is only the first step. Validation comes immediately afterward.
If the CSV file is located in another folder, provide its path:
df = pd.read_csv(
"data/students.csv"
)
On Windows, paths may also be written using a raw string:
df = pd.read_csv(
r"C:\Data\students.csv"
)
The raw string notation can help avoid problems caused by backslashes being interpreted as escape characters.
Another option is to use forward slashes:
df = pd.read_csv(
"C:/Data/students.csv"
)
When working on different computers, it is generally better to avoid hard-coded machine-specific paths when possible and use a well-organized project structure.
Never assume that a file loaded correctly just because Python did not display an error.
Run:
print(df.head())
print(df.shape)
print(df.columns)
print(df.dtypes)
df.info()
These checks answer several important questions:
This habit is particularly important when working with business data because a file can load successfully while still being interpreted incorrectly.
Excel is another extremely common data source, especially in business environments.
Pandas provides read_excel():
df = pd.read_excel(
"students.xlsx"
)
print(df.head())
An Excel workbook can contain multiple sheets.
You can specify a sheet:
df = pd.read_excel(
"students.xlsx",
sheet_name="Students"
)
You can also inspect available sheets using the appropriate Excel-reading tools or load specific sheets when the workbook structure is known.
This is particularly useful when a workbook contains separate sheets such as:
Instead of treating the workbook as one giant table, you can load the sheet relevant to your analysis.
Suppose a workbook contains a sheet called Sales 2026.
sales = pd.read_excel(
"company_sales.xlsx",
sheet_name="Sales 2026"
)
print(sales.head())
This loads only that worksheet into the DataFrame.
You can then inspect:
print(sales.shape)
print(sales.columns)
sales.info()
This is an important workflow when working with operational Excel files containing multiple sheets.
JSON is widely used for web applications, APIs, and data exchange.
Pandas provides read_json():
df = pd.read_json(
"students.json"
)
print(df.head())
However, JSON can have many different structures. Some JSON files contain flat records, while others contain nested objects and lists.
Simple JSON data may load directly into a DataFrame, while nested API responses may require additional transformation before they become a clean tabular structure.
Pandas can also read data directly from some accessible URLs.
url = "https://example.com/data.csv"
df = pd.read_csv(url)
print(df.head())
This can be useful when working with publicly available datasets.
However, an analyst should always verify the source, data freshness, structure, and reliability before using externally hosted data in an important analysis.
Not every CSV-style file uses a comma.
Some files use semicolons or tabs.
For a semicolon-separated file:
df = pd.read_csv(
"data.csv",
sep=";"
)
For tab-separated data:
df = pd.read_csv(
"data.tsv",
sep="\t"
)
If you open a file and discover that the entire row appears inside one column, an incorrect delimiter may be one possible reason.
Text files can use different character encodings.
If a CSV contains Hindi, regional languages, or special characters, encoding becomes particularly important.
A file may sometimes need an explicit encoding:
df = pd.read_csv(
"students.csv",
encoding="utf-8"
)
Some files created by older systems may require another encoding depending on how the file was produced.
The important lesson is not to blindly change encoding whenever an error occurs. First understand how the source file was generated and use an encoding appropriate for that source.
Missing values can appear in CSV or Excel files as empty cells or specific text markers.
After loading the data, inspect missing values:
print(df.isna().sum())
You may also encounter values such as:
NA
N/A
NULL
-
Unknown
These representations should not automatically be treated as identical. For example, Unknown may represent a meaningful category, while an empty cell may represent missing information.
If the source uses a specific marker for missing data, Pandas can be configured to recognize it during import.
df = pd.read_csv(
"customers.csv",
na_values=["NA", "N/A", "NULL"]
)
This converts the specified markers into Pandas missing values.
The exact treatment should depend on the meaning of the source values.
Dates are particularly important in Data Analytics.
A CSV might contain:
Order_Date
2026-01-15
2026-02-20
2026-03-05
Depending on the dataset and Pandas version, you can use date parsing options during import or convert the column after loading.
df["Order_Date"] = pd.to_datetime(
df["Order_Date"]
)
Once converted to an appropriate datetime representation, you can perform operations such as extracting the year, month, day, or calculating differences between dates.
Date handling will be covered more deeply later in the course.
Large files may contain many columns, but your analysis may require only a few.
You can load selected columns using usecols:
df = pd.read_csv(
"sales.csv",
usecols=[
"Order_ID",
"Product",
"Sales"
]
)
This can make the analytical workflow more focused and may reduce unnecessary memory usage.
For large datasets, selecting only the columns needed for a particular task can be an important efficiency practice.
For testing or exploration, you may want to load only a limited number of records.
df = pd.read_csv(
"sales.csv",
nrows=1000
)
This can be useful when the full dataset is very large and you first want to understand its structure.
After understanding the file and confirming the import process, you can load the complete dataset.
Sometimes a file contains extra information before the actual table begins.
For example, a spreadsheet export might have a title or explanatory text above the column headers.
In such situations, import options such as skiprows can be useful:
df = pd.read_csv(
"report.csv",
skiprows=2
)
However, this should only be used when you understand the file structure. Skipping the wrong rows can cause the actual header or important records to be interpreted incorrectly.
Normally, Pandas assumes the first row contains column names.
For example:
Name,Age,Marks
Aman,21,82
Priya,22,91
will use Name, Age, and Marks as column names.
If the file does not contain a header, specify:
df = pd.read_csv(
"students.csv",
header=None
)
You can then assign column names:
df.columns = [
"Name",
"Age",
"Marks"
]
Correct header handling is important because an incorrect header configuration can shift the structure of the entire dataset.
Very large files may not fit comfortably into memory if loaded all at once.
Pandas provides chunk-based reading:
chunks = pd.read_csv(
"large_sales.csv",
chunksize=10000
)
for chunk in chunks:
print(chunk.shape)
Here, Pandas processes the file in smaller pieces.
This approach can be useful when working with very large CSV files and performing operations that can be completed incrementally.
For example, you might process each chunk to calculate totals instead of loading every record into memory simultaneously.
After loading any external file, use a validation checklist.
print(df.head())
print(df.tail())
print(df.shape)
print(df.columns)
print(df.dtypes)
print(df.isna().sum())
print(df.duplicated().sum())
Then ask:
This validation step can prevent major analytical mistakes later.
Imagine a company provides a file called sales_2026.csv.
You might begin with:
import pandas as pd
sales = pd.read_csv(
"sales_2026.csv"
)
print(sales.head())
print(sales.shape)
sales.info()
Then check missing values:
print(sales.isna().sum())
Check duplicates:
print(sales.duplicated().sum())
Inspect numerical statistics:
print(sales.describe())
Only after these checks should you begin deeper transformations and analysis.
Create a CSV file named students.csv containing the following columns:
Add at least 10 student records.
Then write Python code to:
The objective is to reproduce a small version of a real analytical workflow rather than simply practice individual commands.
FileNotFoundError
This usually means Python cannot find the file at the specified path. Check the filename, directory, and working directory.
Incorrect delimiter
If all data appears in one column, the source may use a delimiter other than a comma.
Incorrect header
If column names look wrong or the first data record appears to have become the header, inspect the source file and import settings.
Encoding error
If special characters cannot be decoded, investigate the source encoding rather than randomly trying values.
Unexpected data types
A numerical column may be loaded as text because it contains currency symbols, commas, spaces, or inconsistent values. Inspect and clean the source before performing calculations.
You have now learned how to bring external data into Pandas, which is an essential step in practical Data Analytics.
You learned how to load CSV files using read_csv(), Excel workbooks using read_excel(), and JSON data using read_json(). You also learned how to work with file paths, delimiters, headers, encodings, missing-value markers, selected columns, selected rows, date conversion, and large files.
Most importantly, you learned that loading data is not the same as validating data. A file can load successfully while still containing incorrect data types, missing values, duplicate records, incorrect headers, or unexpected formatting.
A professional workflow therefore follows a simple principle:
Load → Inspect → Validate → Clean → Analyze.
This principle will remain important throughout the rest of the Pandas course.
🎉 Chapter 1 — Pandas Fundamentals is now complete.
You now have the foundation required to work with real datasets in Pandas.