create_pandas_dataframe_agent#

langchain_experimental.agents.agent_toolkits.pandas.base.create_pandas_dataframe_agent(llm: Runnable[PromptValue | str | Sequence[BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]], BaseMessage | str], df: Any, agent_type: AgentType | Literal['openai-tools', 'tool-calling'] = AgentType.ZERO_SHOT_REACT_DESCRIPTION, callback_manager: BaseCallbackManager | None = None, prefix: str | None = None, suffix: str | None = None, input_variables: List[str] | None = None, verbose: bool = False, return_intermediate_steps: bool = False, max_iterations: int | None = 15, max_execution_time: float | None = None, early_stopping_method: str = 'force', agent_executor_kwargs: Dict[str, Any] | None = None, include_df_in_prompt: bool | None = True, number_of_head_rows: int = 5, extra_tools: Sequence[BaseTool] = (), engine: Literal['pandas', 'modin'] = 'pandas', allow_dangerous_code: bool = False, **kwargs: Any) AgentExecutor[source]#

Construct a Pandas agent from an LLM and dataframe(s).

Security Notice:

This agent relies on access to a python repl tool which can execute arbitrary code. This can be dangerous and requires a specially sandboxed environment to be safely used. Failure to run this code in a properly sandboxed environment can lead to arbitrary code execution vulnerabilities, which can lead to data breaches, data loss, or other security incidents.

Do not use this code with untrusted inputs, with elevated permissions, or without consulting your security team about proper sandboxing!

You must opt-in to use this functionality by setting allow_dangerous_code=True.

Parameters:
  • llm (Runnable[PromptValue | str | Sequence[BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]], BaseMessage | str]) – Language model to use for the agent. If agent_type is “tool-calling” then llm is expected to support tool calling.

  • df (Any) – Pandas dataframe or list of Pandas dataframes.

  • agent_type (AgentType | Literal['openai-tools', 'tool-calling']) – One of “tool-calling”, “openai-tools”, “openai-functions”, or “zero-shot-react-description”. Defaults to “zero-shot-react-description”. “tool-calling” is recommended over the legacy “openai-tools” and “openai-functions” types.

  • callback_manager (BaseCallbackManager | None) – DEPRECATED. Pass “callbacks” key into ‘agent_executor_kwargs’ instead to pass constructor callbacks to AgentExecutor.

  • prefix (str | None) – Prompt prefix string.

  • suffix (str | None) – Prompt suffix string.

  • input_variables (List[str] | None) – DEPRECATED. Input variables automatically inferred from constructed prompt.

  • verbose (bool) – AgentExecutor verbosity.

  • return_intermediate_steps (bool) – Passed to AgentExecutor init.

  • max_iterations (int | None) – Passed to AgentExecutor init.

  • max_execution_time (float | None) – Passed to AgentExecutor init.

  • early_stopping_method (str) – Passed to AgentExecutor init.

  • agent_executor_kwargs (Dict[str, Any] | None) – Arbitrary additional AgentExecutor args.

  • include_df_in_prompt (bool | None) – Whether to include the first number_of_head_rows in the prompt. Must be None if suffix is not None.

  • number_of_head_rows (int) – Number of initial rows to include in prompt if include_df_in_prompt is True.

  • extra_tools (Sequence[BaseTool]) – Additional tools to give to agent on top of a PythonAstREPLTool.

  • engine (Literal['pandas', 'modin']) – One of “modin” or “pandas”. Defaults to “pandas”.

  • allow_dangerous_code (bool) – bool, default False This agent relies on access to a python repl tool which can execute arbitrary code. This can be dangerous and requires a specially sandboxed environment to be safely used. Failure to properly sandbox this class can lead to arbitrary code execution vulnerabilities, which can lead to data breaches, data loss, or other security incidents. You must opt in to use this functionality by setting allow_dangerous_code=True.

  • **kwargs (Any) – DEPRECATED. Not used, kept for backwards compatibility.

Returns:

An AgentExecutor with the specified agent_type agent and access to a PythonAstREPLTool with the DataFrame(s) and any user-provided extra_tools.

Return type:

AgentExecutor

Example

from langchain_openai import ChatOpenAI
from langchain_experimental.agents import create_pandas_dataframe_agent
import pandas as pd

df = pd.read_csv("titanic.csv")
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
agent_executor = create_pandas_dataframe_agent(
    llm,
    df,
    agent_type="tool-calling",
    verbose=True
)

Examples using create_pandas_dataframe_agent