All Course > Data Science With Fastapi > Building Rest Apis For Data Science Applications Nov 12, 2024

Handling Data with FastAPI and Pandas: CSV, JSON, and DataFrames

In the previous lesson, we explored how to create your first FastAPI endpoint. We built a simple API that returned a message, and we learned how to set up routes and run the server. Now, we will dive deeper into handling data, which is a core part of building data science applications. In this lesson, we will focus on working with CSV files, JSON data, and Pandas DataFrames in FastAPI. These skills are essential for building APIs that can process and return data efficiently.

Processing CSV Data in FastAPI

I recently worked on a project where I needed to build an API that could read a CSV file, process the data, and return it as a JSON response. The CSV file contained sales data, and the goal was to calculate the total sales for each product category. I used FastAPI and Pandas to achieve this. FastAPI made it easy to handle the HTTP requests, while Pandas simplified the data processing. This use-case taught me how powerful FastAPI and Pandas can be when used together for data science applications.

Loading and Processing CSV Files

To start, we need to load a CSV file into a Pandas DataFrame. FastAPI allows us to upload files using the File and UploadFile classes. Here’s how you can do it:

from fastapi import FastAPI, File, UploadFile
import pandas as pd

app = FastAPI()

@app.post("/upload-csv/")
async def upload_csv(file: UploadFile = File(...)):
    df = pd.read_csv(file.file)
    return {"filename": file.filename, "data": df.to_dict()}

In this example, the API endpoint /upload-csv/ accepts a CSV file, reads it into a DataFrame, and returns the data as a dictionary. This is a simple way to handle CSV files in FastAPI.

Handling JSON Data in FastAPI

JSON is a common format for sending and receiving data in APIs. FastAPI makes it easy to work with JSON data. Let’s say we want to accept JSON data, process it, and return a response. Here’s an example:

from fastapi import FastAPI
from pydantic import BaseModel
import pandas as pd

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float
    quantity: int

@app.post("/process-json/")
async def process_json(item: Item):
    data = {"name": item.name, "total_cost": item.price * item.quantity}
    df = pd.DataFrame([data])
    return df.to_dict()

In this example, the API accepts JSON data, calculates the total cost, and returns the result as a DataFrame converted to a dictionary. This shows how easy it is to handle JSON data in FastAPI.

Returning DataFrames as API Responses

Sometimes, you may want to return a DataFrame directly as an API response. FastAPI can handle this seamlessly. Here’s how you can return a DataFrame as a JSON response:

from fastapi import FastAPI
import pandas as pd

app = FastAPI()

@app.get("/get-dataframe/")
async def get_dataframe():
    data = {"product": ["A", "B", "C"], "sales": [100, 200, 150]}
    df = pd.DataFrame(data)
    return df.to_dict()

This endpoint returns a DataFrame as a JSON response. The to_dict() method converts the DataFrame into a format that FastAPI can return.

Combining CSV, JSON, and DataFrames

In real-world applications, you often need to combine different data formats. For example, you might load a CSV file, process it, and return the result as JSON. Here’s an example:

from fastapi import FastAPI, File, UploadFile
import pandas as pd

app = FastAPI()

@app.post("/process-csv/")
async def process_csv(file: UploadFile = File(...)):
    df = pd.read_csv(file.file)
    total_sales = df["sales"].sum()
    return {"total_sales": total_sales}

This endpoint reads a CSV file, calculates the total sales, and returns the result as JSON. It shows how you can combine different data formats in a single API.

Conclusion

In this lesson, we learned how to handle CSV files, JSON data, and DataFrames in FastAPI. We explored practical examples, such as uploading a CSV file, processing JSON data, and returning DataFrames as API responses. These skills are crucial for building data science applications with FastAPI. In the next lesson, we will take this a step further by deploying machine learning models with FastAPI. Stay tuned to learn how to integrate machine learning into your APIs!

Comments

There are no comments yet.

Write a comment

You can use the Markdown syntax to format your comment.