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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
