Home > Database > Mysql Tutorial > How to Convert SQL Query Results to a Pandas DataFrame?

How to Convert SQL Query Results to a Pandas DataFrame?

Barbara Streisand
Release: 2024-12-04 20:15:13
Original
516 people have browsed it

How to Convert SQL Query Results to a Pandas DataFrame?

Converting SQL Query Results to Pandas Data Structure

Introduction

To facilitate data analysis and manipulation, it is often necessary to convert data retrieved from a SQL database into a Pandas data structure. This article will guide you through the process of achieving this.

Identifying the Return Type

The connection.execute() function in the provided code returns a SQLAlchemy ResultProxy. This object represents the result of the query as an iterable of tuples, where each tuple corresponds to a row in the result.

Converting to Pandas Data Structure

To convert the result tuples into a Pandas DataFrame, you can use the DataFrame constructor:

import pandas as pd

df = pd.DataFrame(resoverall.fetchall())
Copy after login

The fetchall() method returns a list of tuples representing the query results. The DataFrame constructor takes this list as an argument and generates a DataFrame with the tuples as rows.

Setting Column Names

By default, the DataFrame will use generic column names like "0", "1", etc. To assign meaningful column names, use the columns attribute:

df.columns = resoverall.keys()
Copy after login

The resoverall.keys() returns a list of column names from the query result. Assigning this list to the DataFrame.columns attribute sets the column names.

Alternative with Type Conversion

To additionally parse and convert the column types to match the SQL schema, you can use the following approach:

import numpy as np
from sqlalchemy import types

df = pd.DataFrame(resoverall.fetchall())
for column in resoverall.keys():
    df[column] = df[column].astype(types.type_map[resoverall.scalar_types[column]])
Copy after login

This method ensures that the DataFrame column types are consistent with the SQL schema.

The above is the detailed content of How to Convert SQL Query Results to a Pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template