Pandas AI 是一個Python 庫,它為流行的Python 數據分析和操作工具Pandas 庫添加了生成人工智能功能,旨在與Pandas 結合使用,而不是它的替代品。
Demo
安裝
用法
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",
)
可以在示例目錄中找到更多示例。
#Pandas #AI首頁文檔和下載 #為Pandas #庫加入AI #功能 #Development details