[파이썬] Peewee CSV 및 DataFrame 익스포트/임포트

Peewee

Peewee is a lightweight and expressive Object Relational Mapping (ORM) library for Python. It provides a simple and intuitive way to interact with your database, making it easier to perform CRUD operations (Create, Read, Update, Delete) on your data.

In this blog post, we’ll explore how to export and import data using Peewee in CSV format, as well as how to work with DataFrames in Python.

Exporting data to CSV with Peewee

Peewee provides a convenient method tuples() that returns the query result as a list of tuples. We can use this method to fetch the data from the database and then export it to a CSV file.

Here’s an example of exporting data to a CSV file using Peewee:

import csv
from peewee import *

db = SqliteDatabase('mydatabase.db')

class Person(Model):
    name = CharField()
    age = IntegerField()

    class Meta:
        database = db

# Fetch data from the database
data = Person.select().tuples()

# Export data to CSV
with open('persons.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['Name', 'Age'])  # Write header
    writer.writerows(data)  # Write data rows

In this example, we define a Person model using Peewee, which represents a table in our database. We then fetch the data using Person.select().tuples() and save it to a variable called data. We open a CSV file called persons.csv in write mode and use the csv.writer object to write the data to the CSV file.

Importing data from CSV with Peewee

To import data from a CSV file into a database using Peewee, we need to read the CSV file and insert the data into the respective table.

Here’s an example of importing data from a CSV file using Peewee:

import csv
from peewee import *

db = SqliteDatabase('mydatabase.db')

class Person(Model):
    name = CharField()
    age = IntegerField()

    class Meta:
        database = db

# Read data from CSV
data = []
with open('persons.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    next(reader)  # Skip header row
    for row in reader:
        data.append(row)

# Import data into table
with db.atomic():
    Person.insert_many(data).execute()

In this example, we open the CSV file persons.csv in read mode and use the csv.reader object to read its contents. We skip the header row using next(reader) and then loop through each row, appending it to the data list.

We use Person.insert_many(data).execute() to insert the data into the Person table in the database. The atomic() method ensures that all insertions are performed atomically, improving efficiency and performance.

Working with DataFrames

A DataFrame is a two-dimensional table-like data structure in Python, commonly used in data analysis and manipulation. Working with DataFrames can provide additional flexibility and functionality when dealing with data.

To export data from Peewee to a DataFrame, we can use the pandas library.

Here’s an example:

import pandas as pd
from peewee import *

db = SqliteDatabase('mydatabase.db')

class Person(Model):
    name = CharField()
    age = IntegerField()

    class Meta:
        database = db

# Fetch data from the database
query = Person.select()
data = list(query.dicts())  # Convert to list of dictionaries

# Convert data to DataFrame
df = pd.DataFrame(data)

# Export DataFrame to CSV
df.to_csv('persons.csv', index=False)

In this example, we fetch data from the Person table using Person.select(), and then convert the query result to a list of dictionaries using query.dicts(). We use this list to create a DataFrame using pd.DataFrame(data).

Finally, we export the DataFrame to a CSV file using df.to_csv('persons.csv', index=False).

This allows us to leverage the power of pandas and perform advanced data analysis and manipulation operations on our data.

Conclusion

Peewee provides convenient methods for exporting and importing data to and from CSV files. Additionally, working with DataFrames using libraries like pandas can give us more flexibility in working with the exported data.

By combining Peewee with CSV and DataFrame operations, we can easily interact with data in our database, export it to other formats, and perform advanced data analysis and manipulation tasks in Python.

I hope you found this blog post helpful in understanding how to export and import data with Peewee in CSV format, as well as working with DataFrames in Python.