我正在将数据从MySQL数据库导入Pandas数据框架。以下摘录是我正在使用的代码:
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)
当我打印数据框时,它确实可以正确表示数据,但我的问题是,是否还可以保留列名?这是示例输出:
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
我想做的是保留列名,它将替换pandas列索引。例如,与MySQL表中一样,列名将不是:0,而是:“ First_column”。是否有解决这个问题的好方法?还是有比我更有效的从MySQL导入数据到Pandas数据框中的方法?
IMO,使用熊猫从MySQL服务器读取数据会更加高效:
from sqlalchemy import create_engine import pymysql 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)
这也应该照顾列名…