Home Artificial Intelligence 8 ChatGPT Prompts For Continuously Done Pandas Operations Final words

8 ChatGPT Prompts For Continuously Done Pandas Operations Final words

1
8 ChatGPT Prompts For Continuously Done Pandas Operations
Final words

A fast option to get things done with Pandas

Photo by Karsten Winegeart on Unsplash

We’ve all heard about ChatGPT. It’s not only on the tech industry’s radar, but in addition making headlines in the broader press. I incessantly get friends reaching out to ask if I’ve heard about it. And yes, I sure have — I take advantage of it day by day.

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

Recently, I made a decision to leverage ChatGPT for the Pandas operations I recurrently perform for data cleansing and evaluation. In this text, I’ll share and guide you thru 8 prompt examples where I asked ChatGPT how one can get things done with Pandas.

Prompt: You’re a Python tutor teaching me the Pandas library. I will likely be asking you how one can do a specific task with Pandas and expecting you to clarify it to me. Also show me the code along together with your explanation.

I gave it the structure of the DataFrame when it comes to column names and data types before I start asking questions.

Prompt: Let me first inform you in regards to the DataFrame I actually have. Then, I’ll 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 info 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 likely be using a sample dataset that I prepared with mock data. You possibly can download it from my datasets repo. It’s the one named “sales_data_with_stores.csv”.

Here is the DataFrame I actually have so that you can reference easily:

The primary 5 rows of df (image by writer)

We will now start on Pandas tasks.

Prompt: Tell me how one can calculate the common last week sales for every store.

This is a straightforward one-liner but can still take a while to determine in the event you just began learning Pandas. Here is the reply of ChatGPT:

(image by writer)

I will likely be copy-pasting the code snippets in case you should 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 an in depth explanation together with the code when it comes to how the duty done and the kind of the output.

Prompt: Tell me how one can find the overall profit within the last month for every store.

For this task, it must determine how one can calculate the profit first. Let’s see it’s response:

(image by writer)
# 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()

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here