Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing data from a MySQL database into a Pandas data frame including column names [duplicate]

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?

like image 818
vFlav Avatar asked Jun 09 '16 15:06

vFlav


People also ask

How to write data in pandas Dataframe to a mySQL table?

A DataFrame in Pandas is a data structure for storing data in tabular form, i.e., in rows and columns. This article describes how to write the data in a Pandas DataFrame to a MySQL table. Consider a DataFrame with three records like below. You can create a database table in MySQL and insert this data using the to_sql () function in Pandas.

How to import data from a table to a data frame?

The best-opted way will be directly importing the table to the data frame. That will be easier for analysis data against all perspectives. To connect MySQL using pandas, need to install package ‘mysql-connector-python’ as below command.

What do I need to import a pandas file?

You’ll also need the Datetime package. When importing the Pandas package the convention is to use the command import pandas as pd which allows you to call Pandas functions by prefixing them with pd. instead of pandas.. Comma Separated Value or CSV files are likely to be the file format you encounter most commonly in data science.

How to connect MySQL with pandas in Python?

That will be easier for analysis data against all perspectives. To connect MySQL using pandas, need to install package ‘mysql-connector-python’ as below command. Package installation console


1 Answers

IMO it would be much more efficient to use pandas for reading data from your MySQL server:

from sqlalchemy import create_engine import pandas as pd  db_connection_str = 'mysql+pymysql://mysql_user:mysql_password@mysql_host/mysql_db' db_connection = create_engine(db_connection_str)  df = pd.read_sql('SELECT * FROM table_name', con=db_connection) 

this should also take care of column names...

like image 75
MaxU - stop WAR against UA Avatar answered Sep 25 '22 17:09

MaxU - stop WAR against UA