公众号:尤而小屋
作者:Peter
修改:Peter

大家好,我是Peter~

重视小屋的朋友肯定都知道小编一直在坚持写pandas相关的文章,最近的一篇请参考:

Pandas是一个根据python和numpy的数据剖析库,特别擅长数据处理和数据剖析。

今年AI在大模型方面非常炽热,尤其是ChatGPT等东西的呈现,让我们的编程愈加智(失)能(业)。

最近,就有一位大佬将Pandas和AI强强联手,帮助我们愈加快速便捷地剖析数据。

GitHub项目地址:github.com/gventuri/pa…

pandas-ai火了

Pandas-ai现身

Github官方解说什么是pandas-ai:

Pandas AI is a Python library that adds generative artificial intelligence capabilities to Pandas, the popular data analysis and manipulation tool. It is designed to be used in conjunction with Pandas, and is not a replacement for it.

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

安装非常简单:

pip install pandasai  # 命令行
!pip install pandasai  # jupyter notebook中

运用

1、导入相关的库:

import pandas as pd
from pandasai import PandasAI
from pandasai.llm.openai import OpenAI

2、模拟生成数据:

import pandas as pd
from pandasai import PandasAI
df = pd.DataFrame({
    "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
    "gdp": [19294482071552, 2891615567872, 2411255037952, 3435817336832, 1745433788416, 1181205135360, 1607402389504, 1490967855104, 4380756541440, 14631844184064],
    "happiness_index": [6.94, 7.16, 6.66, 7.07, 6.38, 6.4, 7.23, 7.22, 5.87, 5.12]
})

3、初始化一个llm模型

from pandasai.llm.openai import OpenAI
llm = OpenAI()

4、调用大模型实例,运用提示promote

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

显示成果为:

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

另一个实例:

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

绘图功用也是轻松搞定:

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