.ipynb
.pdf
Pandas Dataframe Agent
 Contents 
Using ZERO_SHOT_REACT_DESCRIPTION
Using OpenAI Functions
Multi DataFrame Example
Pandas Dataframe Agent#
This notebook shows how to use agents to interact with a pandas dataframe. It is mostly optimized for question answering.
NOTE: this agent calls the Python agent under the hood, which executes LLM generated Python code - this can be bad if the LLM generated Python code is harmful. Use cautiously.
from langchain.agents import create_pandas_dataframe_agent
from langchain.chat_models import ChatOpenAI
from langchain.agents.agent_types import AgentType
from langchain.llms import OpenAI
import pandas as pd
df = pd.read_csv('titanic.csv')
Using ZERO_SHOT_REACT_DESCRIPTION#
This shows how to initialize the agent using the ZERO_SHOT_REACT_DESCRIPTION agent type. Note that this is an alternative to the above.
agent = create_pandas_dataframe_agent(OpenAI(temperature=0), df, verbose=True)
Using OpenAI Functions#
This shows how to initialize the agent using the OPENAI_FUNCTIONS agent type. Note that this is an alternative to the above.
agent = create_pandas_dataframe_agent(
    ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613"), 
    df, 
    verbose=True, 
    agent_type=AgentType.OPENAI_FUNCTIONS
)
agent.run("how many rows are there?")
> Entering new  chain...
Invoking: `python_repl_ast` with `df.shape[0]`
891There are 891 rows in the dataframe.
> Finished chain.
'There are 891 rows in the dataframe.'
agent.run("how many people have more than 3 siblings")
> Entering new AgentExecutor chain...
rtdocs_stable/api.python.langchain.com/en/stable/modules/agents/toolkits/examples/pandas.html