Skip to main content

Library for converting pandas dataframes to pydantic models

Project description

pandas-to-pydantic

Library for converting pandas dataframes into pydantic models

PyPI - Version PyPI - Python Version


Table of Contents

Installation

pip install pandas-to-pydantic

Example

Using this example test data

import pandas as pd
from pydantic import BaseModel
from pandas_to_pydantic import dataframeToPydantic

# Declare pydantic models
class Book(BaseModel):
    BookID: int
    Title: str
    Genre: str
    PublishedYear: int
    AvailableCopies: int


class Author(BaseModel):
    AuthorID: int
    AuthorName: str
    AuthorBirthdate: str
    BookList: list[Book]


class Library(BaseModel):
    LibraryID: int
    LibraryName: str
    Location: str
    EstablishedYear: int
    BookCollectionSize: int
    AuthorList: list[Author]

# Input data is a pandas dataframe
data = pd.read_csv(FILE_PATH)

# Convert pandas dataframe to a pydantic root model
libraryListRoot = dataframeToPydantic(data, Library)

# Access data as a list of pydantic models
libraryListRoot.root

# Access data as a list of dict
libraryListRoot.model_dump()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pandas_to_pydantic-0.0.3.tar.gz (7.0 kB view hashes)

Uploaded Source

Built Distribution

pandas_to_pydantic-0.0.3-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page