On this tutorial, we’ll discover ways to harness the ability of Google’s Gemini fashions alongside the pliability of Pandas. We are going to carry out each simple and complex knowledge analyses on the traditional Titanic dataset. By combining the ChatGoogleGenerativeAI consumer with LangChain’s experimental Pandas DataFrame agent, we’ll arrange an interactive “agent” that may interpret natural-language queries. It’s going to examine knowledge, compute statistics, uncover correlations, and generate visible insights, with out writing guide code for every activity. We’ll stroll by way of fundamental exploration steps (like counting rows or computing survival charges). We are going to delve into superior analyses akin to survival charges by demographic segments and fare–age correlations. Then we’ll evaluate modifications throughout a number of DataFrames. Lastly, we’ll construct customized scoring and pattern-mining routines to extract novel insights.
!pip set up langchain_experimental langchain_google_genai pandas
import os
import pandas as pd
import numpy as np
from langchain.brokers.agent_types import AgentType
from langchain_experimental.brokers.agent_toolkits import create_pandas_dataframe_agent
from langchain_google_genai import ChatGoogleGenerativeAI
os.environ["GOOGLE_API_KEY"] = "Use Your Personal API Key"
First, we set up the required libraries, langchain_experimental, langchain_google_genai, and pandas, utilizing pip to allow the DataFrame agent and Google Gemini integration. Then import the core modules. Subsequent, set your GOOGLE_API_KEY atmosphere variable, and we’re able to instantiate a Gemini-powered Pandas agent for conversational knowledge evaluation.
def setup_gemini_agent(df, temperature=0, mannequin="gemini-1.5-flash"):
llm = ChatGoogleGenerativeAI(
mannequin=mannequin,
temperature=temperature,
convert_system_message_to_human=True
)
agent = create_pandas_dataframe_agent(
llm=llm,
df=df,
verbose=True,
agent_type=AgentType.OPENAI_FUNCTIONS,
allow_dangerous_code=True
)
return agent
This helper perform initializes a Gemini-powered LLM consumer with our chosen mannequin and temperature. Then it wraps it right into a LangChain Pandas DataFrame agent that may execute natural-language queries (together with “harmful” code) in opposition to our DataFrame. Merely go in our DataFrame to get again an interactive agent prepared for conversational evaluation.
def load_and_explore_data():
print("Loading Titanic Dataset...")
df = pd.read_csv(
"https://uncooked.githubusercontent.com/pandas-dev/pandas/fundamental/doc/knowledge/titanic.csv"
)
print(f"Dataset form: {df.form}")
print(f"Columns: {listing(df.columns)}")
return df
This perform fetches the Titanic CSV instantly from the Pandas GitHub repo. It additionally prints out its dimensions and column names for a fast sanity verify. Then it returns the loaded DataFrame so we will instantly start our exploratory evaluation.
def basic_analysis_demo(agent):
print("nBASIC ANALYSIS DEMO")
print("=" * 50)
queries = [
"How many rows and columns are in the dataset?",
"What's the survival rate (percentage of people who survived)?",
"How many people have more than 3 siblings?",
"What's the square root of the average age?",
"Show me the distribution of passenger classes"
]
for question in queries:
print(f"nQuery: {question}")
strive:
end result = agent.invoke(question)
print(f"Consequence: {end result['output']}")
besides Exception as e:
print(f"Error: {e}")
This demo routine kicks off a “Primary Evaluation” session by printing a header. Then it iterates by way of a set of widespread exploratory queries, like dataset dimensions, survival charges, household counts, and sophistication distributions, in opposition to our Titanic DataFrame agent. For every natural-language immediate, it invokes the agent. Later, it captures its output and prints both the end result or an error.
def advanced_analysis_demo(agent):
print("nADVANCED ANALYSIS DEMO")
print("=" * 50)
advanced_queries = [
"What's the correlation between age and fare?",
"Create a survival analysis by gender and class",
"What's the median age for each passenger class?",
"Find passengers with the highest fares and their details",
"Calculate the survival rate for different age groups (0-18, 18-65, 65+)"
]
for question in advanced_queries:
print(f"nQuery: {question}")
strive:
end result = agent.invoke(question)
print(f"Consequence: {end result['output']}")
besides Exception as e:
print(f"Error: {e}")
This “Superior Evaluation” perform prints a header, then runs a sequence of extra subtle queries. It computes correlations, performs stratified survival analyses, calculates median statistics, and conducts detailed filtering in opposition to our Gemini-powered DataFrame agent. It loop-invokes every natural-language immediate, captures the agent’s responses, and prints the outcomes (or errors). Thus, it demonstrates how simply we will leverage conversational AI for deeper, segmented insights into our dataset.
def multi_dataframe_demo():
print("nMULTI-DATAFRAME DEMO")
print("=" * 50)
df = pd.read_csv(
"https://uncooked.githubusercontent.com/pandas-dev/pandas/fundamental/doc/knowledge/titanic.csv"
)
df_filled = df.copy()
df_filled["Age"] = df_filled["Age"].fillna(df_filled["Age"].imply())
agent = setup_gemini_agent([df, df_filled])
queries = [
"How many rows in the age column are different between the two datasets?",
"Compare the average age in both datasets",
"What percentage of age values were missing in the original dataset?",
"Show summary statistics for age in both datasets"
]
for question in queries:
print(f"nQuery: {question}")
strive:
end result = agent.invoke(question)
print(f"Consequence: {end result['output']}")
besides Exception as e:
print(f"Error: {e}")
This demo illustrates spin up a Gemini-powered agent over a number of DataFrames. On this case, it consists of the unique Titanic knowledge and a model with lacking ages imputed. So, we will ask cross-dataset comparability questions (like variations in row counts, average-age comparisons, missing-value percentages, and side-by-side abstract statistics) utilizing easy natural-language prompts.
def custom_analysis_demo(agent):
print("nCUSTOM ANALYSIS DEMO")
print("=" * 50)
custom_queries = [
"Create a risk score for each passenger based on: Age (higher age = higher risk), Gender (male = higher risk), Class (3rd class = higher risk), Family size (alone or large family = higher risk). Then show the top 10 highest risk passengers who survived",
"Analyze the 'deck' information from the cabin data: Extract deck letter from cabin numbers, Show survival rates by deck, Which deck had the highest survival rate?",
"Find interesting patterns: Did people with similar names (same surname) tend to survive together? What's the relationship between ticket price and survival? Were there any age groups that had 100% survival rate?"
]
for i, question in enumerate(custom_queries, 1):
print(f"nCustom Evaluation {i}:")
print(f"Question: {question[:100]}...")
strive:
end result = agent.invoke(question)
print(f"Consequence: {end result['output']}")
besides Exception as e:
print(f"Error: {e}")
This routine kicks off a “Customized Evaluation” session by strolling by way of three complicated, multi-step prompts. It builds a passenger risk-scoring mannequin, extracts and evaluates deck-based survival charges, and mines surname-based survival patterns and fare/age outliers. Thus, we will see how simply our Gemini-powered agent handles bespoke, domain-specific investigations with simply natural-language queries.
def fundamental():
print("Superior Pandas Agent with Gemini Tutorial")
print("=" * 60)
if not os.getenv("GOOGLE_API_KEY"):
print("Warning: GOOGLE_API_KEY not set!")
print("Please set your Gemini API key as an atmosphere variable.")
return
strive:
df = load_and_explore_data()
print("nSetting up Gemini Agent...")
agent = setup_gemini_agent(df)
basic_analysis_demo(agent)
advanced_analysis_demo(agent)
multi_dataframe_demo()
custom_analysis_demo(agent)
print("nTutorial accomplished efficiently!")
besides Exception as e:
print(f"Error: {e}")
print("Ensure you have put in all required packages and set your API key.")
if __name__ == "__main__":
fundamental()
The primary() perform serves as the place to begin for the tutorial. It verifies that our Gemini API secret’s set, hundreds and explores the Titanic dataset, and initializes the conversational Pandas agent. It then sequentially runs the fundamental, superior, multi-DataFrame, and customized evaluation demos. Lastly, it wraps the complete workflow in a strive/besides block to catch and report any errors earlier than signaling profitable completion.
df = pd.read_csv("https://uncooked.githubusercontent.com/pandas-dev/pandas/fundamental/doc/knowledge/titanic.csv")
agent = setup_gemini_agent(df)
agent.invoke("What elements most strongly predicted survival?")
agent.invoke("Create an in depth survival evaluation by port of embarkation")
agent.invoke("Discover any fascinating anomalies or outliers within the knowledge")
Lastly, we instantly load the Titanic knowledge, instantiate our Gemini-powered Pandas agent, and hearth off three one-off queries. We determine key survival predictors, break down survival by embarkation port, and uncover anomalies or outliers. We obtain all this with out modifying any of our demo features.
In conclusion, combining Pandas with Gemini through a LangChain DataFrame agent transforms knowledge exploration from writing boilerplate code into crafting clear, natural-language queries. Whether or not we’re computing abstract statistics, constructing customized danger scores, evaluating a number of DataFrames, or drilling into nuanced survival analyses, the transformation is obvious. With only a few traces of setup, we achieve an interactive analytics assistant that may adapt to new questions on the fly. It may possibly floor hidden patterns and speed up our workflow.
Take a look at the Pocket book. All credit score for this analysis goes to the researchers of this undertaking. Additionally, be happy to comply with us on Twitter and don’t neglect to affix our 99k+ ML SubReddit and Subscribe to our Publication.
Asif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is dedicated to harnessing the potential of Synthetic Intelligence for social good. His most up-to-date endeavor is the launch of an Synthetic Intelligence Media Platform, Marktechpost, which stands out for its in-depth protection of machine studying and deep studying information that’s each technically sound and simply comprehensible by a large viewers. The platform boasts of over 2 million month-to-month views, illustrating its recognition amongst audiences.