Fundamentals 3 min read

How to Export Oracle Query Results to Excel with Proper Column Headers Using Pandas

This article explains how to retrieve data from an Oracle database with cx_Oracle, convert it to a pandas DataFrame, and correctly write the results to an Excel file with column headers, providing a complete code example and a practical solution to a common data‑processing issue.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Export Oracle Query Results to Excel with Proper Column Headers Using Pandas

Introduction

In a Python community a user asked how to use pandas to retrieve data from an Oracle database and write it to an Excel file with correct column headers.

Implementation

The original script imported cx_Oracle and pandas, executed a SELECT statement, converted the result to a DataFrame, printed it, and saved it with to_excel. However the column titles were missing.

import cx_Oracle
import pandas as pd
conn = cx_Oracle.connect('username','password','127.0.0.1:1521/dbname')
cursor = conn.cursor()
sql = 'select xingming 姓名, fenshu 分数 from a01_fenshu '
cursor.execute(sql)
res = cursor.fetchall()
data = pd.DataFrame(list(res))
print(data)
data.to_excel('demo.xlsx', sheet_name='test')
cursor.close()
conn.close()

A revised approach sets the DataFrame column names explicitly before exporting, which resolves the issue.

Conclusion

The article demonstrates a concrete solution for handling pandas data extraction from Oracle and exporting to Excel with proper headers, helping learners solve similar problems.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonOracleExcelpandasdata-processingcode-example
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.