Dataset Python: Work with Databases the Pandas Way

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Dataset Python: Simple Database Management in the Pandas Style

What is the Dataset Library?

Dataset is a Python library that provides a user-friendly interface for working with SQL databases without writing SQL queries. It lets you interact with databases as easily as you would with Python dictionaries, automatically creating tables and managing data structures on the fly.

Key benefits of Dataset:

  • Automatic table and schema creation
  • Simple, SQL-free API
  • Support for multiple database systems via SQLAlchemy
  • Intuitive and readable syntax
  • Minimal configuration required
  • Excellent compatibility with Pandas

Installing and Setting Up Dataset

Installing the Library

pip install dataset

You may need additional drivers for specific databases:

# For PostgreSQL
pip install psycopg2-binary

# For MySQL
pip install pymysql

# For SQLite, the driver is already included in Python

Connecting to Different Databases

import dataset

# SQLite (local database)
db = dataset.connect('sqlite:///mydatabase.db')

# PostgreSQL
db = dataset.connect('postgresql://user:password@localhost:5432/dbname')

# MySQL
db = dataset.connect('mysql://user:password@localhost/dbname')

# In-memory (for testing)
db = dataset.connect('sqlite:///:memory:')

Core Principles of Dataset

Dataset operates on these core principles:

  1. Automatic table creation — tables are created on first access
  2. Dynamic schema — the table structure is generated automatically when you insert data
  3. Dictionary interface — data is handled as Python dictionaries
  4. No SQL required — all operations are performed through Python methods

Creating and Managing Tables

Accessing a Table

# Get a table (it is created automatically if it doesn't exist)
users_table = db['users']

# Alternative method
users_table = db.get_table('users')

Creating a Table with Custom Settings

# Create a table with a custom primary key
db.create_table('products', primary_id='product_id', primary_type=db.types.string)

Inserting Data

Inserting a Single Record

table = db['users']

# Insert one user
table.insert({
    'name': 'John Smith',
    'email': 'john@example.com',
    'age': 30,
    'city': 'New York'
})

Bulk Insert

# Insert multiple records
users_data = [
    {'name': 'Anna Johnson', 'email': 'anna@example.com', 'age': 25},
    {'name': 'Peter Williams', 'email': 'peter@example.com', 'age': 35},
    {'name': 'Maria Garcia', 'email': 'maria@example.com', 'age': 28}
]

table.insert_many(users_data)

Upsert Operations

# Insert or update (if a record with this email already exists)
table.upsert({
    'name': 'John Smith',
    'email': 'john@example.com',
    'age': 31,
    'city': 'San Francisco'
}, ['email'])

Searching and Filtering Data

Retrieving All Records

# Get all records
all_users = list(table.all())
for user in all_users:
    print(f"{user['name']} - {user['email']}")

Searching with Conditions

# Find one record
user = table.find_one(name='John Smith')

# Find all records matching a condition
young_users = list(table.find(age={'<': 30})                    

Recommendations