首页 > 解决方案 > 从另一个文件导入一个 pd.read_sql() DataFrame 以进一步处理它

问题描述

我想从 read_datasets.py 文件中导入一个数据框并在 main.py 中处理它。另外使用 mysql 连接的函数。

主要.py:

    import mysql.connector
    import pandas as pd



def con():
    connection = None
    
    try:
        # declaration of default mysql settings
        connection = mysql.connector.connect(
        host="xx",
        user="xx",
        passwd="xx",
        db="xx",
        )
    # If connection is not successful    
    except:
        print("cant connect to database")
        return 0
    
    # if connection is successfull
    print("connected")
    # Making Cursor Object For Query Execution
    cursor = connection.cursor()

if __name__ == '__main__':

  print("actually in main.py")


  #talk to connection mysql
  temp1 = pd.read_sql("SELECT xx FROM xx", con() )
  # filter all NAN Vlaues in the Dataframe
   temp1 = temp1.dropna()

在 read_datasets.py 中:

import pandas as pd
from main import con

temp1 = pd.read_sql("SELECT xx FROM xx", con)
# filter all NAN Vlaues in the Dataframe
temp1 = temp1.dropna()

我得到了错误:

AttributeError:“NoneType”对象没有属性“光标”

标签: pythonmysqlpandasdataframe

解决方案


我只有使用 SQLAlchemy 执行此操作的经验,所以我将发布如何使用该包执行此操作:

from sqlalchemy import create_engine

def db_connection(self):
    engine = create_engine('mysql+pymysql://{0}:{1}@{2}:3306/{3}'.format(username, password, host, db_name), echo=False)
    connection = engine.connect()
    return connection

^我没有定义密码等变量,但请注意,上面的代码不起作用,因为你需要这样做!

然后您可以致电:

pd.read_sql("SQL query here", db_connection())

推荐阅读