AWS SDK for pandas

28 - Amazon DynamoDB

Writing Data

from datetime import datetime
from decimal import Decimal
from pathlib import Path

import pandas as pd
from boto3.dynamodb.conditions import Attr, Key

import awswrangler as wr

Writing DataFrame

table_name = "movies"

df = pd.DataFrame(
        "title": ["Titanic", "Snatch", "The Godfather"],
        "year": [1997, 2000, 1972],
        "genre": ["drama", "caper story", "crime"],
wr.dynamodb.put_df(df=df, table_name=table_name)

Writing CSV file

filepath = Path("items.csv")
df.to_csv(filepath, index=False)
wr.dynamodb.put_csv(path=filepath, table_name=table_name)

Writing JSON files

filepath = Path("items.json")
df.to_json(filepath, orient="records")
wr.dynamodb.put_json(path="items.json", table_name=table_name)

Writing list of items

items = df.to_dict(orient="records")
wr.dynamodb.put_items(items=items, table_name=table_name)

Reading Data

Read Items

[ ]:
# Limit Read to 5 items
wr.dynamodb.read_items(table_name=table_name, max_items_evaluated=5)

# Limit Read to Key expression
    table_name=table_name, key_condition_expression=(Key("title").eq("Snatch") & Key("year").eq(2000))

Read PartiQL

    query=f"SELECT * FROM {table_name} WHERE title=? AND year=?",
    parameters=["Snatch", 2000],
year genre title
0 2000 caper story Snatch

Executing statements

title = "The Lord of the Rings: The Fellowship of the Ring"
year =
genre = "epic"
rating = Decimal("9.9")
plot = "The fate of Middle-earth hangs in the balance as Frodo and eight companions begin their journey to Mount Doom in the land of Mordor."

# Insert items
    statement=f"INSERT INTO {table_name} VALUE {{'title': ?, 'year': ?, 'genre': ?, 'info': ?}}",
    parameters=[title, year, genre, {"plot": plot, "rating": rating}],

# Select items
    statement=f'SELECT * FROM "{table_name}" WHERE title=? AND year=?',
    parameters=[title, year],

# Update items
    statement=f'UPDATE "{table_name}" SET info.rating=? WHERE title=? AND year=?',
    parameters=[Decimal(10), title, year],

# Delete items
    statement=f'DELETE FROM "{table_name}" WHERE title=? AND year=?',
    parameters=[title, year],

Deleting items

wr.dynamodb.delete_items(items=items, table_name="table")