添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I am importing data from a MySQL database into a Pandas data frame. The following excerpt is the code that I am using:

import mysql.connector as sql

import pandas as pd

db_connection = sql.connect(host='hostname', database='db_name', user='username', password='password')

db_cursor = db_connection.cursor()

db_cursor.execute('SELECT * FROM table_name')

table_rows = db_cursor.fetchall()

df = pd.DataFrame(table_rows)

When I print the data frame it does properly represent the data but my question is, is it possible to also keep the column names? Here is an example output:

0   1   2     3     4     5     6     7     8

0  :ID[giA0CqQcx+(9kbuSKV== NaN NaN  None  None  None  None  None  None

1  lXB+jIS)DN!CXmj>0(P8^]== NaN NaN  None  None  None  None  None  None

2  lXB+jIS)DN!CXmj>0(P8^]== NaN NaN  None  None  None  None  None  None

3  lXB+jIS)DN!CXmj>0(P8^]== NaN NaN  None  None  None  None  None  None

4  lXB+jIS)DN!CXmj>0(P8^]== NaN NaN  None  None  None  None  None  None

What I would like to do is keep the column name, which would replace the pandas column indexes. For example, instead of having 0, the column name would be: "First_column" as in the MySQL table. Is there a good way to go about this? or is there a more efficient approach of importing data from MySQL into a Pandas data frame than mine?

  • E&ICT MNIT - Data Science and Machine Learning | IIT Madras - Advance Certification in Data Science and AI | E&ICT IIT Guwahati - Cloud Computing & DevOps | E&ICT IIT Guwahati - Cyber Security | E&ICT IIT Guwahati - Software Engineering & Application Development | E&ICT IIT Guwahati - Big Data Analytics | E&ICT IIT Guwahati - Full Stack Web Development | E&ICT MNIT - AI and Machine Learning | E&ICT IIT Guwahati - UI UX Design Strategy | IIT Madras - Data Analytics for Business | E&ICT IIT Roorkee - Cloud Computing & DevOps | E&ICT MNIT - Cyber Security & Ethical Hacking | E&ICT MNIT - Business Analyst & Project Management

    Browse By Domains

    Big Data Analytics Courses | Data Science Courses | Business Intelligence courses | Best Salesforce Courses | Cloud Computing Courses | Best Digital Marketing Courses | Programming Courses Online | Database Certification Courses | Project Management Certification Courses | Best Web Development Certification courses | Automation Trainings | UI UX Course

    Popular Courses

    Data Science Courses | Best Artificial Certification Intelligence Course | Machine Learning Training | Python Course | DevOps Certification Course | AWS Training and Certification | Azure Certification | AWS DevOps Training | Azure DevOps Certification Course | Cyber Security Course | Ethical Hacking Course | Big Data Hadoop Certification | SQL Course | Salesforce Admin Certification | Online Salesforce Developer Certification | Selenium Training Online | Business Analyst Course Online | Data Analyst Certification course | Best Microsoft Power BI Certification Course | Tableau Course Online | Linux certification Course | Data Analytics Certification Course | Digital Marketing Course | Online Business Analytics Course | Online Investment Banking Course | Online Supply Chain Course | Online Electric Vehicle Course

    Degree Courses

    M. Tech in AI & ML | M. Sc. in Data Science | MBA in Big Data Management | M. Sc. in Artificial Intelligence | MBA in Business Administration | MBA in International Marketing | Master’s in Computer Science | MBA in Finance and Accounting | Master’s in Engineering Management | Master of Science in Data Science | MSC in Data Science