Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Machine Learning»Tutorial to Create a Data Science Agent: A Code Implementation using gemini-2.0-flash-lite model through Google API, google.generativeai, Pandas and IPython.display for Interactive Data Analysis

    Tutorial to Create a Data Science Agent: A Code Implementation using gemini-2.0-flash-lite model through Google API, google.generativeai, Pandas and IPython.display for Interactive Data Analysis

    March 28, 2025

    In this tutorial, we demonstrate the integration of Python’s robust data manipulation library Pandas with Google Cloud’s advanced generative capabilities through the google.generativeai package and the Gemini Pro model. By setting up the environment with the necessary libraries, configuring the Google Cloud API key, and leveraging the IPython display functionalities, the code provides a step-by-step approach to building a data science agent analyzing a sample sales dataset. The example shows how to convert a DataFrame into markdown format and then use natural language queries to generate insights about the data, highlighting the potential of combining traditional data analysis tools with modern AI-driven methods.

    Copy CodeCopiedUse a different Browser
    !pip install pandas google-generativeai --quiet

    First, we install the Pandas and google-generativeai libraries quietly, setting up the environment for data manipulation and AI-powered analysis.

    Copy CodeCopiedUse a different Browser
    import pandas as pd
    import google.generativeai as genai
    from IPython.display import Markdown

    We import Pandas for data manipulation, google.generativeai for accessing Google’s generative AI capabilities, and Markdown from IPython.display to render markdown-formatted outputs.

    Copy CodeCopiedUse a different Browser
    GOOGLE_API_KEY = "Use Your API Key Here"
    genai.configure(api_key=GOOGLE_API_KEY)
    
    
    model = genai.GenerativeModel('gemini-2.0-flash-lite')

    We assign a placeholder API key, configure the google.generativeai client with it, and initialize the ‘gemini-2.0-flash-lite’ GenerativeModel for generating content.

    Copy CodeCopiedUse a different Browser
    data = {'Product': ['Laptop', 'Mouse', 'Keyboard', 'Monitor', 'Webcam', 'Headphones'],
            'Category': ['Electronics', 'Electronics', 'Electronics', 'Electronics', 'Electronics', 'Electronics'],
            'Region': ['North', 'South', 'East', 'West', 'North', 'South'],
            'Units Sold': [150, 200, 180, 120, 90, 250],
            'Price': [1200, 25, 75, 300, 50, 100]}
    sales_df = pd.DataFrame(data)
    
    
    print("Sample Sales Data:")
    print(sales_df)
    print("-" * 30)

    Here, we create a Pandas DataFrame named sales_df containing sample sales data for various products, and then print the DataFrame followed by a separator line to visually distinguish the output.

    Copy CodeCopiedUse a different Browser
    def ask_gemini_about_data(dataframe, query):
        """
        Asks the Gemini Pro model a question about the given Pandas DataFrame.
    
    
        Args:
            dataframe: The Pandas DataFrame to analyze.
            query: The natural language question about the DataFrame.
    
    
        Returns:
            The response from the Gemini Pro model as a string.
        """
        prompt = f"""You are a data analysis agent. Analyze the following pandas DataFrame and answer the question.
    
    
        DataFrame:
        ```
        {dataframe.to_markdown(index=False)}
        ```
    
    
        Question: {query}
    
    
        Answer:
        """
        response = model.generate_content(prompt)
        return response.text

    Here, we construct a markdown-formatted prompt from a Pandas DataFrame and a natural language query, then use the Gemini Pro model to generate and return an analytical response.

    Copy CodeCopiedUse a different Browser
    # Query 1: What is the total number of units sold across all products?
    query1 = "What is the total number of units sold across all products?"
    response1 = ask_gemini_about_data(sales_df, query1)
    print(f"Question 1: {query1}")
    print(f"Answer 1:n{response1}")
    print("-" * 30)
    Query 1 Output
    Copy CodeCopiedUse a different Browser
    # Query 2: Which product had the highest number of units sold?
    query2 = "Which product had the highest number of units sold?"
    response2 = ask_gemini_about_data(sales_df, query2)
    print(f"Question 2: {query2}")
    print(f"Answer 2:n{response2}")
    print("-" * 30)
    Query 2 Output
    Copy CodeCopiedUse a different Browser
    # Query 3: What is the average price of the products?
    query3 = "What is the average price of the products?"
    response3 = ask_gemini_about_data(sales_df, query3)
    print(f"Question 3: {query3}")
    print(f"Answer 3:n{response3}")
    print("-" * 30)
    Query 3 Output
    Copy CodeCopiedUse a different Browser
    # Query 4: Show me the products sold in the 'North' region.
    query4 = "Show me the products sold in the 'North' region."
    response4 = ask_gemini_about_data(sales_df, query4)
    print(f"Question 4: {query4}")
    print(f"Answer 4:n{response4}")
    print("-" * 30)
    Query 4 Output
    Copy CodeCopiedUse a different Browser
    # Query 5. More complex query: Calculate the total revenue for each product.
    query5 = "Calculate the total revenue (Units Sold * Price) for each product and present it in a table."
    response5 = ask_gemini_about_data(sales_df, query5)
    print(f"Question 5: {query5}")
    print(f"Answer 5:n{response5}")
    print("-" * 30)
    Query 5 Output

    In conclusion, the tutorial successfully illustrates how the synergy between Pandas, the google.generativeai package, and the Gemini Pro model can transform data analysis tasks into a more interactive and insightful process. The approach simplifies querying and interpreting data and opens up avenues for advanced use cases such as data cleaning, feature engineering, and exploratory data analysis. By harnessing these state-of-the-art tools within the familiar Python ecosystem, data scientists can enhance their productivity and innovation, making it easier to derive meaningful insights from complex datasets.


    Here is the Colab Notebook. Also, don’t forget to follow us on Twitter and join our Telegram Channel and LinkedIn Group. Don’t Forget to join our 85k+ ML SubReddit.

    The post Tutorial to Create a Data Science Agent: A Code Implementation using gemini-2.0-flash-lite model through Google API, google.generativeai, Pandas and IPython.display for Interactive Data Analysis appeared first on MarkTechPost.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleAmazon Bedrock Guardrails image content filters provide industry-leading safeguards, helping customer block up to 88% of harmful multimodal content: Generally available today
    Next Article Meta Reality Labs Research Introduces Sonata: Advancing Self-Supervised Representation Learning for 3D Point Clouds

    Related Posts

    Machine Learning

    Salesforce AI Releases BLIP3-o: A Fully Open-Source Unified Multimodal Model Built with CLIP Embeddings and Flow Matching for Image Understanding and Generation

    May 16, 2025
    Machine Learning

    How to Evaluate Jailbreak Methods: A Case Study with the StrongREJECT Benchmark

    May 16, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Catch two of Capcom’s best Monster Hunter titles on PC for massive discounts to tide you over while waiting for Monster Hunter Wilds

    Development
    Apple sheds $700 billion as Microsoft regains the world’s most valuable company title — amid President Trump’s steep tariffs on Chinese imports

    Apple sheds $700 billion as Microsoft regains the world’s most valuable company title — amid President Trump’s steep tariffs on Chinese imports

    News & Updates

    Lernstick – secure and mobile learning and working environment

    Development

    ByteDance Introduces UI-TARS: A Native GUI Agent Model that Integrates Perception, Action, Reasoning, and Memory into a Scalable and Adaptive Framework

    Machine Learning

    Highlights

    Gemini, following Copilot’s footsteps, is now available on Google Workspace side panel

    June 27, 2024

    Google confirmed the general availability of Gemini on Google Workspace side panel, including services like…

    Angular Specialists for Hire: Average Salary and Recruiting Guide

    March 28, 2024

    How to secure your GitHub Actions workflows with CodeQL

    January 9, 2025

    TA547 Phishing Attack Hits German Firms with Rhadamanthys Stealer

    April 11, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.