langchain_experimental.agents.agent_toolkits.pandas.base.create_pandas_dataframe_agent

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

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

Parameters
  • llm (Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], Union[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 (Union[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 (Optional[BaseCallbackManager]) – DEPRECATED. Pass “callbacks” key into ‘agent_executor_kwargs’ instead to pass constructor callbacks to AgentExecutor.

  • prefix (Optional[str]) – Prompt prefix string.

  • suffix (Optional[str]) – Prompt suffix string.

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

  • verbose (bool) – AgentExecutor verbosity.

  • return_intermediate_steps (bool) – Passed to AgentExecutor init.

  • max_iterations (Optional[int]) – Passed to AgentExecutor init.

  • max_execution_time (Optional[float]) – Passed to AgentExecutor init.

  • early_stopping_method (str) – Passed to AgentExecutor init.

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

  • include_df_in_prompt (Optional[bool]) – 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”.

  • **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