The super combination of Pandas and ChatGPT is awesome!

From the official account: Data STUDIO

Python Pandas is an open source toolkit that provides data manipulation and analysis capabilities for Python programming. This library has become a must-have tool for data scientists and analysts. It provides an efficient way to manage structured data (Series and DataFrame).

In the field of artificial intelligence, Pandas is often used as a preprocessing step in machine learning and deep learning processes. By providing data cleansing, reshaping, merging, and aggregation, Pandas can convert raw datasets into structured, ready-to-use 2-dimensional tables that can be fed into artificial intelligence algorithms.

b7df8dfb4c9b261777af598ac1895378.png

Project address: https://github.com/gventuri/pandas-ai

Install Pandas AI using pip

pip install pandasai

Use OpenAI to import PandasAI

In the next step, we will import the pandasai library we installed earlier, and then import the LLM (Large Language Model) feature. As of May 2023, pandasai only supports OpenAI models, which we will use to understand the data.

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": [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]
})

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

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

To use the OpenAI API, you must generate your own unique API key.

Because of the characteristics of pandas, we can not only process csv files, but also connect to relational databases, such as pgsql:

# creating the uri and connecting to database
 pg_conn = "postgresql://YOUR URI HERE"
 
 #Query sql database 
 query = """
 SELECT *
 FROM table_name
 """
 
 #Create dataframe named df
 df = pd.read_sql(query,pg_conn)

Then, like the code above, we can talk to it directly:

# Using pandas-ai!
 pandas_ai = PandasAI(llm)
 pandas_ai.run(df, prompt='Place your prompt here)

Of course, you can also let PandasAI do more complex queries. For example, PandasAI can be asked to sum the GDP of the 2 least happy countries:

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

The above code will return the following:

19012600725504

You can also ask PandasAI to draw:

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

ChatGPT, Pandas are powerful tools that when combined can revolutionize the way we interact with and analyze data. ChatGPT, with its advanced natural language processing capabilities, enables more intuitive human-like interactions with data. And PandasAI can enhance the Pandas data analysis experience. By converting complex data manipulation tasks into simple natural language queries, PandasAI makes it easier for users to extract valuable insights from data without writing a lot of code.

This is a new way of programming for those who are not yet familiar with Python or pandas operations/transformations. We don't need to program the task you want to perform, but just talk to the AI ​​agent, tell it explicitly the desired result, and the agent will convert this message into a computer interpretable code, and return the result.


Finally, I would like to recommend our member group. Currently, there are venture capital angel investors, headhunters HR, Douyin big V, emotional bloggers, lawyers, psychological counselors, medical sales, real estate, insurance, piano teachers, operators, business consulting, Students from cross-border e-commerce, construction, data analysts in the Internet industry, back-end development, python testing and other industries will join.

At present, there are 400+ people on the planet, and 41 cheats have been updated in the content of the column. Every day, planets publish their own experiences. You can learn for only one dollar:

Python: 44 lessons of python introductory course + 9 lessons of Django column + interesting practical cases

chatgpt: entry, advanced, fun office, advanced courses

AI painting: Mj's basics, entry, advanced, Xiaohongshu gameplay

You can also meet a lot of great people!

Join to send ChatGPT independent account

973f7c592cf5355a202dab7ab3a434ce.jpeg

Also send ChatGPT advanced video courses

The original price is 99, and now it is free to send planet members

465265176fca155fa60a3d5aaacaab88.jpeg

WeChat long press to try the content

If you are not satisfied within three days, you can directly refund! ! !

8cd8e5c8bddc3a21f643577a686abfc0.png

推荐阅读:
入门: 最全的零基础学Python的问题  | 零基础学了8个月的Python  | 实战项目 |学Python就是这条捷径
干货:爬取豆瓣短评,电影《后来的我们》 | 38年NBA最佳球员分析 |   从万众期待到口碑扑街!唐探3令人失望  | 笑看新倚天屠龙记 | 灯谜答题王 |用Python做个海量小姐姐素描图 |碟中谍这么火,我用机器学习做个迷你推荐系统电影
趣味:弹球游戏  | 九宫格  | 漂亮的花 | 两百行Python《天天酷跑》游戏!
AI: 会做诗的机器人 | 给图片上色 | 预测收入 | 碟中谍这么火,我用机器学习做个迷你推荐系统电影
小工具: Pdf转Word,轻松搞定表格和水印! | 一键把html网页保存为pdf!|  再见PDF提取收费! | 用90行代码打造最强PDF转换器,word、PPT、excel、markdown、html一键转换 | 制作一款钉钉低价机票提示器! |60行代码做了一个语音壁纸切换器天天看小姐姐!|

Guess you like

Origin blog.csdn.net/cainiao_python/article/details/131039437