当前位置:   article > 正文

【Python模块】pandas-AI火了!_pandasai 可以用那些模型

pandasai 可以用那些模型

大家好,我是Peter~

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

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

今年AI在大模型方面十分火热,尤其是ChatGPT等工具的出现,让我们的编程更加智(失)能(业)。

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

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

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
  • 2
  • 3

使用

1、导入相关的库:

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

  • 1
  • 2
  • 3
  • 4

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]
})

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3、初始化一个llm模型

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

  • 1
  • 2
  • 3

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

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

  • 1
  • 2
  • 3

显示结果为:

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

另一个实例:

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

19012600725504

  • 1
  • 2
  • 3
  • 4

绘图功能也是轻松搞定:

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

  • 1
  • 2
  • 3
  • 4
  • 5
---------------------------END---------------------------

题外话

在这里插入图片描述

感兴趣的小伙伴,赠送全套Python学习资料,包含面试题、简历资料等具体看下方。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/214665
推荐阅读
相关标签