{
  "source_file": "0930",
  "source_platform": "Hashnode",
  "author": "Wesley Kambale",
  "title_listed": "PandasAI: The Generative AI Library",
  "keywords": "",
  "domain": "大模型算法",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://kambale.dev/pandasai",
  "publish_date": "2023-06-16",
  "corpus_stage": "",
  "notes": "",
  "id": "8fed8912c0da",
  "collected_at": "2026-10-02T14:11:32",
  "title": "Understanding PandasAI: The Generative AI Library",
  "body_markdown": "PandasAI: The Generative AI Library\nPandas AI, the Python library that enhances Pandas with generative artificial intelligence capabilities.\nUpdated\nJuly 18, 2023\n•\n5\nmin read\n•\nView as Markdown\nW\nWesley Kambale\nI'm a Machine Learning Engineer passionate about building production-ready ML systems for the African market.\nWith experience in TensorFlow, Keras, and Python-based workflows, I help teams bridge the gap between machine learning research and real-world deployment—especially on resource-constrained devices.\nI'm also a Google Developer Expert in AI. I regularly speak at tech conferences including PyCon Africa, DevFest Kampala, DevFest Nairobi and more and also write technical articles on AI/ML here.\nPart of series\nMachine Learning\nIntroduction\nWhat is PandasAI?\nPandasAI is an advanced library built on top of the popular Pandas library, designed to provide enhanced functionality for data manipulation, analysis, and AI-driven tasks. With PandasAI, you can efficiently handle large datasets, perform complex operations, and leverage artificial intelligence techniques seamlessly. In this article, we will explore the key features of PandasAI with practical examples and code snippets.\nRead more about\nPandas\nhere.\nKey Features of PandasAI\nPandasAI extends the functionality of Pandas with additional features. Some of the key features are:\nFeature Engineering\n: PandasAI offers a wide range of feature engineering techniques such as one-hot encoding, binning, scaling, and generating new features.\nAI-driven Operations\n: PandasAI integrates with popular AI libraries like scikit-learn and TensorFlow, enabling seamless integration of machine learning and deep learning algorithms with Pandas data-frames.\nExploratory Data Analysis (EDA)\n: It provides various statistical and visualization tools for EDA, including descriptive statistics, correlation analysis, and interactive visualizations.\nTime Series Analysis\n: PandasAI includes powerful tools for handling time series data, such as resampling, lagging, rolling computations, and date-based operations.\nInstallation of PandasAI\nTo install PandasAI, you can use the\npip\npackage manager, which simplifies the process. Run the following command to install PandasAI:\npip install pandasai\nIf you wish to use PandasAI in a Google Colab Notebook like I am doing, you need to run the following commands to install PandasAI and other necessary modules:\n!pip install pandasai\n!pip install langchain\nAfter successfully installing PandasAI, you need to import it along with the Pandas library to start using its enhanced functionalities.\nimport\npandas\nas\npd\nfrom\npandasai\nimport\nPandasAI\nfrom\npandasai.llm.openai\nimport\nOpenAI\nTo use the OpenAI library, we will have to generate an OpenAI API Key\nhere\n. Also, ensure that you set up a paid account for access to OpenAI's Large Language Models (LLM) which are priced per 1,000 tokens.\nFunctionality of PandasAI\nBefore exploring the functionality of PandasAI, we will have to first set up an OpenAI environment and create an instance of PandasAI with the OpenAI environment we created.\n# Loading the API token to OpenAI environment\nenv = OpenAI(api_token=\n'OpenAI API Key'\n)\n# Initializing an instance of PandasAI with OpenAI environment\npandasAi = PandasAI(env)\nData Exploration\nIn this article, we are going to use the employee dataset containing information about employees, including their names, ages, salaries, and departments. However, this dataset has missing values in some of the columns. Below is a screenshot of the dataset:\nTo explore our data, we will now prompt or ask\npandasAi\nby passing in our dataset name and the question we wish to ask. In the code snippet below, we will ask PandasAI to tell us which employees have null values in the dataset.\nquestion =\n\"Which employees have null values?\"\npandasAi.run(data, prompt=question)\nOutput:\nTo check whether PandasAI is correct with the output given from our dataset, we can run a query for null values using Pandas itself with the\nisna()\nfunction.\n# Check for null values using Pandas\ndata = data[data.isna().any(axis=\n1\n)]\n# Print the rows with the null values\nprint(data)\nOutput:\nFrom the above output, we can see that both Pandas and PandasAI return the same rows that have null values in our dataset.\nNext, we can prompt\npandasAi\nto tell us which employee earns more than all the employees in the dataset. We will run the following code:\nquestion =\n\"Who earns more than the others?\"\npandasAi.run(data, prompt=question)\nOutput:\nWith a salary of 2000000, Marvin is the highest-paid employee. And we can see from the output above that PandasAI is correct.\nUp next, we can ask PandasAI to fill in the null values for the employees without salaries. To do this, we are going to run the following code:\nquestion =\n\"Fill in only the null values to 5 figure salaries\"\npandasAi.run(data, prompt=question)\nOutput:\nFrom the above output, PandasAI can fill in the null values with a 5-figure salary for each employee who previously had a null value. The salary is uniform for each, but you can always twist the question/prompt to randomize the numbers.\nIn the next prompt, we can ask PandasAI to tell us how many employees are in different departments. This is crucial if one wishes to analyze employee data to know how employees are distributed across departments. In the code below, we prompt for the number of employees in the Sales department:\nquestion =\n\"How many employees are in the Sales department?\"\npandasAi.run(data, prompt=question)\nOutput:\nIndeed there are 5 employees in the Sales department in our dataset.\nThere's more a data analyst can prompt PandasAI to do in terms of data exploration. As you can realize, we do not run the traditional Pandas codes and queries to analyze our dataset, but instead, supply English language prompts and the generative AI library will use OpenAI's LLM capabilities to return outputs.\nData Visualization\nAs with Pandas, PandasAI can also be used to visualize data with simple prompts supplied to\npandasAi\n. Below, we prompt PandasAI to plot visual charts of our data.\nquestion =\n\"Plot a barplot of all employees and their salaries\"\npandasAi.run(data, prompt=question)\nOutput:\nWe can plot the employee's salaries grouped by their departments and see which departments get more salary amounts compared to other departments. To do that, we twist the prompt as seen below:\nquestion =\n\"Plot a barplot of all employees and their salaries grouped in their departments\"\npandasAi.run(data, prompt=question)\nOutput:\nNext, we can plot a boxplot for the employee's salary and age with the following prompt:\nquestion =\n\"Plot a boxplot out of the employee salary and age\"\npandasAi.run(data, prompt=question)\nOutput:\nWe can also show the relationship between the employee's salaries and their age. To do that, we run the following prompt:\nquestion =\n\"Plot a scatter graph from the employee data\"\npandasAi.run(data, prompt=question)\nOutput:\nConclusion\nPandasAI extends the capabilities of Pandas by providing advanced data manipulation, analysis, and AI-driven operations. In this article, we covered key features, and use cases, and provided examples and code snippets to illustrate the functionality of PandasAI. By leveraging PandasAI, you can streamline your data preprocessing pipeline and seamlessly integrate AI techniques into your Pandas workflows.\nResources\nGoogle Colab Notebook\nDataset\nPandasAI Repository\nPandasAI Docs\n#\npandas\n#\ngenerative-ai\n#\ndata-analysis",
  "metadata": {
    "title": "Understanding PandasAI: The Generative AI Library",
    "description": "Pandas AI, the Python library that enhances Pandas with generative artificial intelligence capabilities.",
    "keywords": null,
    "author": null,
    "og:title": "Understanding PandasAI: The Generative AI Library",
    "og:description": "Pandas AI, the Python library that enhances Pandas with generative artificial intelligence capabilities.",
    "og:url": "https://kambale.dev/pandasai",
    "og:site_name": "Wesley Kambale",
    "og:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1689704553884/0698933f-1af7-4dfd-b702-59a60c492b26.png",
    "og:image:width": "1200",
    "og:image:height": "630",
    "og:type": "article",
    "twitter:card": "summary_large_image",
    "twitter:creator": "https://twitter.com/WesKambale",
    "twitter:title": "Understanding PandasAI: The Generative AI Library",
    "twitter:description": "Pandas AI, the Python library that enhances Pandas with generative artificial intelligence capabilities.",
    "twitter:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1689704553884/0698933f-1af7-4dfd-b702-59a60c492b26.png",
    "article:published_time": "2023-06-16T12:45:47.534Z",
    "article:modified_time": "2023-07-18T18:22:58.258Z",
    "article:author": "https://kambale.dev/@kambale"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/8fed8912c0da/code/body-000.txt",
      "chars": 21,
      "language": "text"
    },
    {
      "file": "attachments/8fed8912c0da/code/body-001.txt",
      "chars": 45,
      "language": "text"
    },
    {
      "file": "attachments/8fed8912c0da/code/body-002.txt",
      "chars": 89,
      "language": "text"
    },
    {
      "file": "attachments/8fed8912c0da/code/body-003.txt",
      "chars": 176,
      "language": "text"
    },
    {
      "file": "attachments/8fed8912c0da/code/body-004.txt",
      "chars": 83,
      "language": "text"
    },
    {
      "file": "attachments/8fed8912c0da/code/body-005.txt",
      "chars": 124,
      "language": "text"
    },
    {
      "file": "attachments/8fed8912c0da/code/body-006.txt",
      "chars": 81,
      "language": "text"
    },
    {
      "file": "attachments/8fed8912c0da/code/body-007.txt",
      "chars": 99,
      "language": "text"
    },
    {
      "file": "attachments/8fed8912c0da/code/body-008.txt",
      "chars": 97,
      "language": "text"
    },
    {
      "file": "attachments/8fed8912c0da/code/body-009.txt",
      "chars": 100,
      "language": "text"
    },
    {
      "file": "attachments/8fed8912c0da/code/body-010.txt",
      "chars": 129,
      "language": "text"
    },
    {
      "file": "attachments/8fed8912c0da/code/body-011.txt",
      "chars": 99,
      "language": "text"
    },
    {
      "file": "attachments/8fed8912c0da/code/body-012.txt",
      "chars": 93,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1689704553884/0698933f-1af7-4dfd-b702-59a60c492b26.png",
      "bytes": 81434,
      "reused": true,
      "local_path": "attachments/8fed8912c0da/images/000.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1689704553884/0698933f-1af7-4dfd-b702-59a60c492b26.png",
      "alt": "PandasAI: The Generative AI Library",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686866709849/6b89f818-c64e-44c4-b284-62ca5d7f3143.png",
      "bytes": 10337,
      "reused": true,
      "local_path": "attachments/8fed8912c0da/images/002.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686866709849/6b89f818-c64e-44c4-b284-62ca5d7f3143.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686866175977/51decd4f-4589-471c-96b2-386c6c7d2c89.png",
      "bytes": 11101,
      "reused": true,
      "local_path": "attachments/8fed8912c0da/images/003.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686866175977/51decd4f-4589-471c-96b2-386c6c7d2c89.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686866485623/fea66a6e-724b-48bb-a865-205929a2e647.png",
      "bytes": 3646,
      "reused": true,
      "local_path": "attachments/8fed8912c0da/images/004.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686866485623/fea66a6e-724b-48bb-a865-205929a2e647.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686867077123/09762bd3-7f81-4e11-9fc1-e86335e80167.png",
      "bytes": 1054,
      "reused": true,
      "local_path": "attachments/8fed8912c0da/images/005.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686867077123/09762bd3-7f81-4e11-9fc1-e86335e80167.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686868481972/ae10a13f-3ba0-4d68-a85f-7fa2f729df83.png",
      "bytes": 3878,
      "reused": true,
      "local_path": "attachments/8fed8912c0da/images/006.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686868481972/ae10a13f-3ba0-4d68-a85f-7fa2f729df83.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686869264768/a66fc227-636d-449f-99c7-f3f128ae91d6.png",
      "bytes": 1760,
      "reused": true,
      "local_path": "attachments/8fed8912c0da/images/007.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686869264768/a66fc227-636d-449f-99c7-f3f128ae91d6.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686869942147/67d4a043-c129-41b6-8166-804d6efe110a.png",
      "bytes": 25666,
      "reused": true,
      "local_path": "attachments/8fed8912c0da/images/008.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686869942147/67d4a043-c129-41b6-8166-804d6efe110a.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686870752231/7ce98ec4-bb0f-4bad-9d7b-bab6f1a5a4e8.png",
      "bytes": 14552,
      "reused": true,
      "local_path": "attachments/8fed8912c0da/images/009.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686870752231/7ce98ec4-bb0f-4bad-9d7b-bab6f1a5a4e8.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686871372531/535ee4ca-5199-4bff-819a-546dae7a9550.png",
      "bytes": 8730,
      "reused": true,
      "local_path": "attachments/8fed8912c0da/images/010.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686871372531/535ee4ca-5199-4bff-819a-546dae7a9550.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686871578521/01c665ca-a57f-43e0-a274-5b3f55c4c2f1.png",
      "bytes": 10510,
      "reused": true,
      "local_path": "attachments/8fed8912c0da/images/011.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1686871578521/01c665ca-a57f-43e0-a274-5b3f55c4c2f1.png",
      "alt": "",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://kambale.dev/pandasai.md",
      "label": "View as Markdown",
      "type": "a"
    },
    {
      "href": "https://hashnode.com/@kambale",
      "label": "W",
      "type": "a"
    },
    {
      "href": "https://twitter.com/WesKambale",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://linkedin.com/in/WesKambale",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://kambale.dev/series/ml-series",
      "label": "Part of series Machine Learning",
      "type": "a"
    },
    {
      "href": "https://kambale.dev/pandas-the-gateway-to-data-exploration-and-visualization",
      "label": "Pandas",
      "type": "a"
    },
    {
      "href": "https://platform.openai.com/account/api-keys",
      "label": "here",
      "type": "a"
    },
    {
      "href": "https://colab.research.google.com/drive/1sB1gAaz6GGlLkmkhP_foxZ3-x4qsZo-z?usp=sharing",
      "label": "Google Colab Notebook",
      "type": "a"
    },
    {
      "href": "https://drive.google.com/file/d/12pV2KysMz0UPrH7GEgH8x3OwQwtJgJkw/view?usp=sharing",
      "label": "Dataset",
      "type": "a"
    },
    {
      "href": "https://github.com/gventuri/pandas-ai",
      "label": "PandasAI Repository",
      "type": "a"
    },
    {
      "href": "https://pandas-ai.readthedocs.io/en/latest/",
      "label": "PandasAI Docs",
      "type": "a"
    },
    {
      "href": "https://kambale.dev/tag/pandas",
      "label": "# pandas",
      "type": "a"
    },
    {
      "href": "https://kambale.dev/tag/generative-ai",
      "label": "# generative-ai",
      "type": "a"
    },
    {
      "href": "https://kambale.dev/tag/data-analysis",
      "label": "# data-analysis",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:50:51Z",
  "source_url": "https://kambale.dev/pandasai",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 7470,
  "expected_body_images": 11,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/8fed8912c0da/pages/source.html",
  "local_html": "attachments/8fed8912c0da/pages/article.html",
  "body_base_path": "attachments/8fed8912c0da/pages/",
  "body_markdown_path": "attachments/8fed8912c0da/pages/article.md"
}