Pandas AI为 Pandas 库加入 AI 功能

联合创作 · 2023-09-25 21:52

Pandas AI 是一个 Python 库,它为流行的 Python 数据分析和操作工具 Pandas 库添加了生成人工智能功能,旨在与 Pandas 结合使用,而不是它的替代品。


Demo


在浏览器中试用 PandasAI:Open in Colab


安装



pip install pandasai


用法


PandasAI 旨在与 Pandas 结合使用。它使 Pandas 具有对话性,允许以 Pandas DataFrames 的形式提出有关数据的问题并获得答案。


例如,您可以要求 PandasAI 查找 DataFrame 中列值大于 5 的所有行,它将返回仅包含这些行的 DataFrame:


import pandas as pd
from pandasai import PandasAI

# Sample DataFrame
df = pd.DataFrame({
"country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
"gdp": [21400000, 2940000, 2830000, 3870000, 2160000, 1350000, 1780000, 1320000, 516000, 14000000],
"happiness_index": [7.3, 7.2, 6.5, 7.0, 6.0, 6.3, 7.3, 7.3, 5.9, 5.0]
})

# Instantiate a LLM
from pandasai.llm.openai import OpenAI
llm = OpenAI()

pandas_ai = PandasAI(llm)
pandas_ai.run(df, prompt='Which are the 5 happiest countries?')

上面的代码将返回以下内容:



0     United States
6 Canada
7 Australia
1 United Kingdom
3 Germany
Name: country, dtype: object

当然,你也可以让 PandasAI 进行更复杂的查询。例如,您可以要求 PandasAI 求出 2 个最不幸福国家的 GDP 总和:



pandas_ai.run(df, prompt='What is the sum of the GDPs of the 2 unhappiest countries?')


上面的代码将返回以下内容:

14516000

也可以请 PandasAI 画图:


pandas_ai.run(
df,
"Plot the histogram of countries showing for each the gpd, using different colors for each bar",
)

Chart


 



可以在示例目录中找到更多示例。

 

浏览 25
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报