Home > Technology peripherals > AI > Make good use of these eight ChatGPT instructions to complete Pandas tasks efficiently

Make good use of these eight ChatGPT instructions to complete Pandas tasks efficiently

PHPz
Release: 2023-06-07 17:39:56
forward
1740 people have browsed it

We have all heard of ChatGPT. It not only received attention from the tech industry, but also made headlines in a wider range of media.

Despite some criticism about its performance and reliability on simpler tasks, ChatGPT performs well on a variety of tasks compared to other large language models (LLMs) and has become a productivity important driving force.

Applying ChatGPT to clean and analyze Pandas data can significantly improve work efficiency. This article introduces 8 prompt examples to teach you how to complete Pandas tasks by asking ChatGPT.

First tip: Define its role

First tip to determine its role:

Tip: You are a Python who taught me how to use the Pandas library tutor. I'm looking forward to you explaining to me how to use Pandas to perform a specific task. Also, please show me the code in your explanation.

Before I started asking questions, I gave the structure of the DataFrame, including column names and data types.

Second Tip

Tip: Let me first tell you about the DataFrame I have. Then I'll start asking questions. Below are the column names and data types given in Python dictionary form. The keys represent column names and the values ​​represent data types.

{'store': dtype('O'), 'product_group': dtype('O'), 'product_code': dtype('int64'), 'stock_qty': dtype(' int64'), 'cost': dtype('float64'), 'price': dtype('float64'), 'last_week_sales': dtype('int64'), 'last_month_sales': dtype('int64')}

A sample data set prepared from simulated data is used here. You can download it from the datasets repository repo (https://github.com/SonerYldrm/datasets). The file is named sales_data_with_stores.csv..

The following is the DataFrame provided for you for your reference:

Make good use of these eight ChatGPT instructions to complete Pandas tasks efficiently

The first 5 rows of the DataFrame

Now we can start working on Pandas Mission accomplished.

Example 1

Hint: Tell me how to calculate the average of last week's sales for each store.

Although this is just a simple line of code, for people who are just starting to learn Pandas, it may take some time to understand. The following is the answer given by ChatGPT:

Make good use of these eight ChatGPT instructions to complete Pandas tasks efficiently

## The test code snippet given by ChatGPT, copy and paste as shown below.

# 导入pandasimport pandas as pd# 假设'df'是你的DataFrameaverage_sales = df.groupby('store')['last_week_sales'].mean()
Copy after login
ChatGPT also provides detailed explanations of how the code is executed and the type of output along with the code.

Example 2

Hint: Tell me how to find the total profit for each store last month.

For this task, ChatGPT needs to figure out how to calculate profits first. Let’s see its response:

Make good use of these eight ChatGPT instructions to complete Pandas tasks efficiently

# 计算每个产品的利润df['last_month_profit'] = (df['price'] - df['cost']) * df['last_month_sales']# 计算每个商店的总利润total_profit = df.groupby('store')['last_month_profit'].sum()
Copy after login
Example 3

When dealing with real-world data, we often encounter the challenge of missing values. Test how ChatGPT handles less straightforward tasks.

Tip: There are some missing values ​​in the inventory quantity column. I want to replace the missing values ​​with the average inventory quantity for that store. Tell me how to accomplish this task.

Make good use of these eight ChatGPT instructions to complete Pandas tasks efficiently

# 计算每个商店的平均库存数量average_stock_qty_per_store = df.groupby('store')['stock_qty'].transform('mean')# 用该商店的平均库存数量替换'stock_qty'中的缺失值df['stock_qty'].fillna(average_stock_qty_per_store, inplace=True)
Copy after login
There are many ways to solve this task, and using the transform function is one of the more effective ways.

The transform function is very important. The reasons for using the transform function to complete this task are as follows:

The transform function is similar to the apply function, but unlike the apply function, the transform function can return the same size as the input. Series, which makes it useful when performing vectorized operations and broadcasting, especially when used with groupby objects.

Example 4

Prompt: Tell me how to find the top three products with the highest sales volume in each store last month.

Make good use of these eight ChatGPT instructions to complete Pandas tasks efficiently

# 首先,按'store'和'last_month_sales'对DataFrame进行排序df_sorted = df.sort_values(['store', 'last_month_sales'], ascending=[True, False])# 然后,按'store'分组,并使用apply方法选择每个组的前3行top_3_products = df_sorted.groupby('store').head(3)
Copy after login

它首先对整个DataFrame进行排序。我们也可以在每个组内进行排序。让我们看看ChatGPT是否知道该如何做到这一点。

示例 5

提示:是否有另一种方法可以在不先对DataFrame进行排序的情况下完成此任务?

Make good use of these eight ChatGPT instructions to complete Pandas tasks efficiently

def top_3_products(group):return group.nlargest(3, 'last_month_sales')top_3_df = df.groupby('store').apply(top_3_products)
Copy after login

更高效的方法可能是首先对整个DataFrame进行排序。此示例只是想观察ChatGPT是否能提供不同的解决方案。

示例 6

提示:我想看到每个商店的上周表现与上月表现进行比较。

这是一个简单的任务,但提示并没有直接告诉ChatGPT如何完成。让我们看看它的表现如何。

Make good use of these eight ChatGPT instructions to complete Pandas tasks efficiently

# 计算上周和上个月每个店的总销售额total_sales_last_week = df.groupby('store')['last_week_sales'].sum()total_sales_last_month = df.groupby('store')['last_month_sales'].sum()# 创建一个新的DataFrame来存储这些信息performance_df = pd.DataFrame({'LastWeekSales': total_sales_last_week,'LastMonthSales': total_sales_last_month,})# 计算从上个月到上周销售额的变化performance_df['SalesChange'] = performance_df['LastWeekSales'] - (performance_df['LastMonthSales'] / 4)
Copy after login

作为对上周与上月的表现相比较的反应,它找到了上周销售额与上月销售额四分之一之间存在差异,这是非常合理的。

它创建的DataFrame如下所示:

Make good use of these eight ChatGPT instructions to complete Pandas tasks efficiently

示例 7

提示:创建一个名为critical_stock的新列,如果stock_qty小于500,则取值为1,否则取值为0。

ChatGPT为这个任务给出了3个选项,以下是我更喜欢的两个:

Make good use of these eight ChatGPT instructions to complete Pandas tasks efficiently

# 第一种选项df['critical_stock'] = df['stock_qty'].apply(lambda x: 1 if x <h2>示例 8</h2><blockquote style="margin-top: 5px; margin-bottom: 5px; padding-left: 1em; margin-left: 0px; border-left: 3px solid rgb(238, 238, 238); opacity: 0.6;"><p>提示:以大小、缺失值、数据类型和列统计信息的形式创建一个DataFrame的摘要。</p></blockquote><p>虽然DataFrame的函数和方法总结起来很简单,但ChatGPT在整理它们时做得非常出色,呈现出整洁清晰的排版。</p><p>如下的是ChatGPT生成的代码:</p><pre class="brush:php;toolbar:false"># 导入必要的库import pandas as pd# DataFrame大小print("DataFrame Size:")print(df.shape)print("\n")# 缺失值print("Missing Values:")print(df.isnull().sum())print("\n")# 数据类型print("Data Types:")print(df.dtypes)print("\n")# 列统计信息print("Column Statistics:")print(df.describe(include='all'))
Copy after login

总结

以上示例展示了ChatGPT如何在数据分析中,尤其是在Pandas中,革命性地改变了常见任务的完成方式。我们不仅提高了效率,还对可以在Pandas中完成的复杂操作有了更丰富的理解。

The above is the detailed content of Make good use of these eight ChatGPT instructions to complete Pandas tasks efficiently. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template