Unit4-GAIA / tools /analyze_csv_file.py
Romain Lembo
Add tools for analyzing CSV and Excel files
f9c2e6d
raw
history blame
696 Bytes
from langchain_core.tools import tool
import pandas
@tool
def analyze_csv_file(file_path: str, query: str) -> str:
"""
Analyze a CSV file using pandas and answer a question about it.
Args:
file_path (str): the path to the CSV file.
query (str): Question about the data
"""
try:
file = pandas.read_csv(file_path)
result = f"CSV file loaded with {len(file)} rows and {len(file.columns)} columns.\n"
result += f"Columns: {', '.join(file.columns)}\n\n"
result += "Summary statistics:\n"
result += str(file.describe())
return result
except Exception as e:
return f"Error analyzing CSV file: {str(e)}"