[Solved] Langchain pandas agent unable to run pandas commands – Pandas

by
Ali Hasan
langchain llama-cpp-python openai-api pandas py-langchain

Quick Fix: To run pandas commands in a Langchain pandas agent, add the following code to provide custom information for your agent: query = "what is the mean of the profit?" query = query + " using tool python_repl_ast" pd_agent.run(query)

The Problem:

Langchain pandas agent is unable to run pandas commands.

The user is trying to use Langchain’s pandas agent to perform some data analysis on a CSV file. However, the agent gets stuck in a recursive loop and is unable to take action on the thought of calculating the mean of the profit column.

The agent’s tools description has been updated to include pandas but it did not help.

The user has also confirmed that the dataframe is loaded into the agent’s local variables.

The issue seems to be that the agent is not able to use the python_repl_ast tool to execute the pandas commands.

The Solutions:

Solution 1: Custom Information for Agent

Langchain agents typically use information provided for context. To specify custom information, add the following code:

query = "what is the mean of the profit?"
query = query + " using tool python_repl_ast"
pd_agent.run(query)

This will provide the pandas agent with the necessary information to execute the required pandas commands using the `python_repl_ast` tool.

Q&A

How is langchain pandas agent unable to use the python_repl_ast for pandas calculation?

Most of the information in Langchain agents is for prompt context. Use custom information for your agent’s prompt.

Where do I put custom information for my agent?

Add custom information to your agent’s prompt. Example included in answer.

What additional information should be added to agent’s prompt?

Specify the tool to use for the calculation in the prompt. Example included in answer.

Video Explanation:

The following video, titled "Chat with a CSV | LangChain Agents Tutorial (Beginners) - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Your browser can't play this video. Learn more ... Pandas DataFrame Agent - Beginner Tutorial. Dave Ebbelaar•12K views · 18:37 · Go to channel ...