Last modified: Sep 07, 2026

How to Load Data in Python

Loading data is the first step in any data analysis task. Python offers many ways to read files and databases. This guide will show you the most common and effective methods. You will learn to handle CSV, Excel, JSON, and SQL data. We will use clean, practical examples. By the end, you will be ready to start your own projects.

Why Data Loading Matters

You cannot analyze what you cannot access. Loading data correctly ensures your analysis is accurate. A wrong import can cause errors or misleading results. Python provides libraries to simplify this process. Mastering data loading is essential for every data analyst. It saves time and prevents frustration.

The most popular library is pandas. It is fast, powerful, and easy to use. Pandas can read many file formats with a single function. Let us explore the core methods for loading data. We will start with the most common format, CSV.

Loading CSV Files

CSV stands for Comma-Separated Values. It is a simple text format. Many systems export data as CSV. Pandas provides the read_csv function for this task. It is the most used method for loading tabular data.

Here is a basic example. We will create a small CSV file and load it.


# First, let's create a sample CSV file
import pandas as pd

# Write a simple CSV file
with open('sample_data.csv', 'w') as f:
    f.write('Name,Age,City\n')
    f.write('Alice,25,New York\n')
    f.write('Bob,30,London\n')
    f.write('Charlie,35,Paris\n')

# Now, load the CSV file using pandas
df = pd.read_csv('sample_data.csv')

# Display the loaded data
print(df)

This code creates a file and then reads it. The output shows a clean table. The read_csv function automatically detects the header row. It also infers data types for each column. This is a powerful feature.


      Name  Age      City
0    Alice   25  New York
1      Bob   30    London
2  Charlie   35     Paris

You can load data from a URL as well. Just pass the URL string to read_csv. This is handy for public datasets. Remember to check if the data has a different delimiter. Use the sep parameter to specify it. For example, use sep=';' for semicolon-separated files.

Reading Excel Files

Excel files are common in business environments. Pandas can read them using the read_excel function. This requires an extra library called openpyxl or xlrd. You can install it with pip. It is a straightforward process.

Excel files can have multiple sheets. The read_excel function allows you to specify the sheet name. Here is how to load data from an Excel file.


# To read Excel files, ensure you have openpyxl installed
# pip install openpyxl

import pandas as pd

# Load data from a specific sheet
df_excel = pd.read_excel('financial_data.xlsx', sheet_name='Sheet1')

# Show the first few rows
print(df_excel.head())

This code reads data from a file named financial_data.xlsx. The head() method shows the first five rows. It is a quick way to preview the data. If you do not specify a sheet name, pandas loads the first sheet by default.

Sometimes, your data might not have headers. You can set header=None and assign column names later. This gives you full control. For large files, consider using the nrows parameter to load a sample. This helps with memory management.

Working with JSON Data

JSON is a popular format for web APIs. It stands for JavaScript Object Notation. It is readable and lightweight. Pandas offers the read_json function to load JSON files. This function converts JSON into a DataFrame.

Here is an example of loading a JSON file. We will create a nested JSON structure.


import pandas as pd
import json

# Create a sample JSON file
data = {
    "employees": [
        {"name": "John", "age": 30, "dept": "Sales"},
        {"name": "Jane", "age": 25, "dept": "Marketing"},
        {"name": "Dave", "age": 35, "dept": "IT"}
    ]
}

# Write to a file
with open('data.json', 'w') as f:
    json.dump(data, f)

# Load the JSON file into a DataFrame
df_json = pd.read_json('data.json')

# The 'employees' key becomes the records
print(df_json)

This code reads the JSON structure. The output is a DataFrame with columns for name, age, and department. JSON data often requires normalization. If your data is deeply nested, use json_normalize. This function flattens the structure into a table.


  age   dept   name
0  30   Sales   John
1  25  Marketing  Jane
2  35     IT   Dave

For APIs, you can use the requests library to fetch data. Then, convert the response to a DataFrame. This is a common workflow in data science. It allows you to analyze live data from the web.

Importing from SQL Databases

Databases store large amounts of structured data. Python can connect to SQL databases using libraries like sqlite3 or SQLAlchemy. Pandas provides the read_sql_query function. This function executes a SQL query and returns a DataFrame.

Here is a simple example with SQLite. SQLite is a lightweight, file-based database. It is perfect for small projects.


import pandas as pd
import sqlite3

# Create a connection to a database (or create it)
conn = sqlite3.connect('mydatabase.db')

# Create a table and insert some data
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS products
                  (id INTEGER PRIMARY KEY, name TEXT, price REAL)''')
cursor.execute("INSERT INTO products (name, price) VALUES ('Laptop', 1200.00)")
cursor.execute("INSERT INTO products (name, price) VALUES ('Mouse', 25.00)")
conn.commit()

# Load data from the database using a SQL query
df_sql = pd.read_sql_query("SELECT * FROM products", conn)

# Close the connection
conn.close()

# Display the data
print(df_sql)

This code creates a database, adds data, and then loads it. The read_sql_query function is powerful. It allows you to use complex SQL joins and filters. This is essential for working with relational data.


   id     name   price
0   1   Laptop  1200.0
1   2    Mouse    25.0

For other databases like PostgreSQL or MySQL, you need a connection string. Use SQLAlchemy to create an engine. Then, pass the engine to the read_sql_query function. This method is consistent across different database systems.

Handling Missing Data

After loading data, you often encounter missing values. These are represented as NaN in pandas. It is important to handle them correctly. You can use the isnull function to detect them. This is a critical step in data cleaning.

Here is how to check for missing data.


# Check for missing values in the DataFrame
print(df.isnull().sum())

# Drop rows with missing values
df_clean = df.dropna()

# Or fill missing values with a specific value
df_filled = df.fillna(0)

The dropna method removes rows with any missing values. The fillna method replaces them. Always understand your data before choosing a strategy. Removing data can lead to bias. Filling with zeros might not be appropriate for all columns.

You can also use the interpolate method for time series data. This fills gaps with a linear estimate. This is a more advanced technique. It helps maintain data trends.

Best Practices for Loading Data

Always preview your data after loading. Use the head() and info() methods. The info method shows data types and memory usage. This helps you catch errors early.

Check the shape of your data with .shape. This returns the number of rows and columns. It is a quick sanity check. If you expect 1000 rows but see 10, something is wrong.

For large files, consider loading in chunks. Use the chunksize parameter in read_csv. This processes the file in parts. It is efficient for memory management. This is a key skill for big data tasks.

If you are working on a specific project, you might need to merge datasets. Use the merge function to combine DataFrames. This is similar to SQL joins. For a deeper dive into analysis techniques, see our Python Data Analysis: A Complete How-To Guide.

Remember to always close database connections. This prevents resource leaks. Use a context manager (the with statement) when possible. It handles cleanup automatically.

Conclusion

Loading data is a fundamental skill in Python. We covered CSV, Excel, JSON, and SQL. Each format has a dedicated pandas function. The process is simple and consistent. Practice with these examples to build confidence.

Start with small files to test your code. Then, scale up to larger datasets. Explore different parameters to customize the loading process. The more you practice, the easier it becomes.

For a broader understanding of data workflows, check out our Python Data Analysis: A Beginner's Guide. If you are preparing for a job, review our Python Data Science Interview Questions Guide. These resources will help you advance your skills.

Now you are ready to load any data file. Use these techniques in your next project. Happy coding!