8 ChatGPT Prompts For Frequently Done Pandas Operations | by Soner Yıldırım | May, 2023

A quick way to get things done with Pandas

Photo by Karsten Winegeart on Unsplash

We’ve all heard about ChatGPT. It’s not just on the tech industry’s radar, but also making headlines in the wider press. I frequently get friends reaching out to ask if I’ve heard about it. And yes, I sure have — I use it every day.

Despite some critiques concerning its performance and reliability on simpler tasks, ChatGPT and other large language models (LLMs) excel in a variety of tasks. For me, they’ve served as a significant productivity booster.

Recently, I decided to leverage ChatGPT for the Pandas operations I regularly carry out for data cleaning and analysis. In this article, I’ll share and guide you through 8 prompt examples where I asked ChatGPT how to get things done with Pandas.

First prompt to define its role:

Prompt: You are a Python tutor teaching me the Pandas library. I will be asking you how to do a particular task with Pandas and expecting you to explain it to me. Also show me the code along with your explanation.

I gave it the structure of the DataFrame in terms of column names and data types before I start asking questions.

Second prompt:

Prompt: Let me first tell you about the DataFrame I have. Then, I will start asking questions. The columns and their data types are given below as a Python dictionary with keys showing column names and values showing the 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’)}

I will be using a sample dataset that I prepared with mock data. You can download it from my datasets repo. It’s the one named “sales_data_with_stores.csv”.

Here is the DataFrame I have for you to reference easily:

The first 5 rows of df (image by author)

We can now start on Pandas tasks.

Example 1

Prompt: Tell me how to calculate the average last week sales for each store.

This is a simple one-liner but can still take some time to figure out if you just started learning Pandas. Here is the answer of ChatGPT:

(image by author)

I will be copy-pasting the code snippets in case you want to test it for yourself.

# Import pandas
import pandas as pd

# Assuming 'df' is your DataFrame
average_sales = df.groupby('store')['last_week_sales'].mean()

ChatGPT also provided a detailed explanation along with the code in terms of how the task done and the type of the output.

Example 2

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

For this task, it needs to figure out how to calculate the profit first. Let’s see it’s response:

(image by author)
# Calculate profit per product
df['last_month_profit'] = (df['price'] - df['cost']) * df['last_month_sales']

# Calculate total profit per store
total_profit = df.groupby('store')['last_month_profit'].sum()

Source link

Leave a Comment