PandasAI 的使命是使数据分析和操作对每个人来说更加高效和易于访问。这包括使连接到数据源并在您的数据分析和操作工作流程中使用它们变得更加容易。

PandasAI 提供了多种连接器,允许您连接到不同的数据源。这些连接器设计得易于使用,即使您不熟悉数据源或 PandasAI。

要使用连接器,首先需要安装所需的依赖项。您可以通过运行以下命令来完成此操作:

# Using poetry (recommended)

poetry add pandasai[connectors]

# Using pip

pip install pandasai[connectors]

观看如何使用连接器的视频:

SQL 连接器

PandasAI 提供以下SQL数据库的连接器:

  • PostgreSQL
  • MySQL
  • 通用SQL
  • 雪花
  • DataBricks
  • GoogleBigQuery
  • 雅虎财经
  • Airtable

此外,PandasAI 提供了一个通用的 SQL 连接器,可用于连接到任何 SQL 数据库。

PostgreSQL 连接器

PostgreSQL 连接器允许您连接到 PostgreSQL 数据库。它的设计旨在易于使用,即使您不熟悉 PostgreSQL 或 PandasAI。

要使用PostgreSQL连接器,您只需将其导入到您的Python代码中,并将其传递给SmartDataframeSmartDatalake对象:

from pandasai import SmartDataframe

from pandasai.connectors import PostgreSQLConnector



postgres_connector = PostgreSQLConnector(

    config={

        "host": "localhost",

        "port": 5432,

        "database": "mydb",

        "username": "root",

        "password": "root",

        "table": "payments",

        "where": [

            # this is optional and filters the data to

            # reduce the size of the dataframe

            ["payment_status", "=", "PAIDOFF"],

        ],

    }

)



df = SmartDataframe(postgres_connector)

df.chat('What is the total amount of payments in the last year?')

MySQL 连接器

与PostgreSQL连接器类似,MySQL连接器允许您连接到MySQL数据库。它的设计旨在易于使用,即使您不熟悉MySQL或PandasAI。

要使用MySQL连接器,您只需将其导入到您的Python代码中,并将其传递给SmartDataframeSmartDatalake对象:

from pandasai import SmartDataframe

from pandasai.connectors import MySQLConnector



mysql_connector = MySQLConnector(

    config={

        "host": "localhost",

        "port": 3306,

        "database": "mydb",

        "username": "root",

        "password": "root",

        "table": "loans",

        "where": [

            # this is optional and filters the data to

            # reduce the size of the dataframe

            ["loan_status", "=", "PAIDOFF"],

        ],

    }

)



df = SmartDataframe(mysql_connector)

df.chat('What is the total amount of loans in the last year?')

Sqlite 连接器

与PostgreSQL和MySQL连接器类似,Sqlite连接器允许您连接到本地的Sqlite数据库文件。它的设计旨在易于使用,即使您不熟悉Sqlite或PandasAI。

要使用Sqlite连接器,您只需将其导入到您的Python代码中,并将其传递给SmartDataframeSmartDatalake对象:

from pandasai import SmartDataframe

from pandasai.connectors import SqliteConnector



connector = SqliteConnector(config={

    "database" : "PATH_TO_DB",

    "table" : "actor",

    "where" :[

        ["first_name","=","PENELOPE"]

    ]

})



df = SmartDataframe(connector)

df.chat('How many records are there ?')

通用SQL连接器

通用SQL连接器允许您连接到任何由SQLAlchemy支持的SQL数据库。

要使用通用的SQL连接器,您只需要将其导入到您的Python代码中,并将其传递给SmartDataframeSmartDatalake对象:

from pandasai.connectors import SQLConnector



sql_connector = SQLConnector(

    config={

        "dialect": "sqlite",

        "driver": "pysqlite",

        "host": "localhost",

        "port": 3306,

        "database": "mydb",

        "username": "root",

        "password": "root",

        "table": "loans",

        "where": [

            # this is optional and filters the data to

            # reduce the size of the dataframe

            ["loan_status", "=", "PAIDOFF"],

        ],

    }

)

Snowflake 连接器

Snowflake 连接器允许您连接到 Snowflake。它与 SQL 连接器非常相似,但它是为 Snowflake 量身定制的。 在生产环境中使用此连接器需要许可证(查看详情)。如果您计划在生产环境中使用它,请联系我们

要使用Snowflake连接器,您只需将其导入到您的Python代码中,并将其传递给SmartDataframeSmartDatalake对象:

from pandasai import SmartDataframe

from pandasai.ee.connectors import SnowFlakeConnector



snowflake_connector = SnowFlakeConnector(

    config={

        "account": "ehxzojy-ue47135",

        "database": "SNOWFLAKE_SAMPLE_DATA",

        "username": "test",

        "password": "*****",

        "table": "lineitem",

        "warehouse": "COMPUTE_WH",

        "dbSchema": "tpch_sf1",

        "where": [

            # this is optional and filters the data to

            # reduce the size of the dataframe

            ["l_quantity", ">", "49"]

        ],

    }

)



df = SmartDataframe(snowflake_connector)

df.chat("How many records has status 'F'?")

DataBricks 连接器

DataBricks 连接器允许您连接到 Databricks。它与 SQL 连接器非常相似,但它是为 Databricks 量身定制的。 在生产环境中使用此连接器需要许可证(查看详情)。如果您计划在生产环境中使用它,请联系我们

要使用DataBricks连接器,您只需要将其导入到您的Python代码中,并将其传递给AgentSmartDataframeSmartDatalake对象:

from pandasai.ee.connectors import DatabricksConnector



databricks_connector = DatabricksConnector(

    config={

        "host": "adb-*****.azuredatabricks.net",

        "database": "default",

        "token": "dapidfd412321",

        "port": 443,

        "table": "loan_payments_data",

        "httpPath": "/sql/1.0/warehouses/213421312",

        "where": [

            # this is optional and filters the data to

            # reduce the size of the dataframe

            ["loan_status", "=", "PAIDOFF"],

        ],

    }

)

GoogleBigQuery 连接器

GoogleBigQuery 连接器允许您连接到 GoogleBigQuery 数据集。它与 SQL 连接器非常相似,但它是为 Google BigQuery 量身定制的。 在生产环境中使用此连接器需要许可证(查看详情)。如果您计划在生产环境中使用它,请联系我们

要使用GoogleBigQuery连接器,您只需将其导入到您的Python代码中,并将其传递给AgentSmartDataframeSmartDatalake对象:

from pandasai.connectors import GoogleBigQueryConnector



bigquery_connector = GoogleBigQueryConnector(

    config={

        "credentials_path" : "path to keyfile.json",

        "database" : "dataset_name",

        "table" : "table_name",

        "projectID" : "Project_id_name",

        "where": [

            # this is optional and filters the data to

            # reduce the size of the dataframe

            ["loan_status", "=", "PAIDOFF"],

        ],

    }

)

雅虎财经连接器

Yahoo Finance 连接器允许您通过简单地传递您想要分析的股票的股票代码来连接到 Yahoo Finance。

要使用Yahoo Finance连接器,您只需将其导入到您的Python代码中,并将其传递给SmartDataframeSmartDatalake对象:

from pandasai import SmartDataframe

from pandasai.connectors.yahoo_finance import YahooFinanceConnector



yahoo_connector = YahooFinanceConnector("MSFT")



df = SmartDataframe(yahoo_connector)

df.chat("What is the closing price for yesterday?")

Airtable 连接器

Airtable 连接器允许您通过简单地传递要分析的表的 base_idtokentable_name 来连接到 Airtable 项目表。

要使用Airtable连接器,您只需将其导入到您的Python代码中,并将其传递给AgentSmartDataframeSmartDatalake对象:

from pandasai.connectors import AirtableConnector

from pandasai import SmartDataframe





airtable_connectors = AirtableConnector(

    config={

        "token": "AIRTABLE_API_TOKEN",

        "table":"AIRTABLE_TABLE_NAME",

        "base_id":"AIRTABLE_BASE_ID",

        "where" : [

            # this is optional and filters the data to

            # reduce the size of the dataframe

            ["Status" ,"=","In progress"]

        ]

    }

)



df = SmartDataframe(airtable_connectors)



df.chat("How many rows are there in data ?")