When crafting a detailed prompt for a Large Language Model (LLM) to assist with extracting data from a DataFrame using SQL queries, it's important to provide context, specify the tools you're using, and outline the problem you want to solve. Here's an example of how you might structure such a prompt:
Prompt:
I am working on a project using Python and I have a pandas DataFrame containing my dataset. I would like to perform data extraction using SQL queries. Below are the details of my specific requirements:
-
DataFrame Description:
- The DataFrame is named
df and contains the following columns:
name: A string representing the name of individuals.
age: An integer denoting their age.
city: A string indicating the city of residence.
-
Objective:
- I want to write and execute SQL queries on this DataFrame to filter, select specific columns, and perform aggregations. Specifically:
- Retrieve records of individuals aged over 30.
- Select only
name and city columns for individuals living in "New York".
- Calculate the average age of individuals grouped by the
city.
-
Tools Available:
- I have
pandasql, which allows SQL queries on pandas DataFrames. Alternatively, I can convert the DataFrame into a SQL table using sqlite3 and then query it.
-
Output:
- I would like the results of these queries to be presented as new pandas DataFrames so I can easily manipulate and analyze the results further using pandas functions.
-
Further Clarifications:
- Please provide a step-by-step guide or examples illustrating how to implement these queries using the tools mentioned above.
Using the above specifications, please generate the necessary Python code and provide explanations as needed for clarity. The goal is to efficiently query the DataFrame using SQL-like syntax to extract the desired subset of data.
Explanation:
-
Descriptive Context: Giving a detailed description of the DataFrame and your objectives helps the LLM understand exactly what you are working with and what you want to achieve.
-
Tool Specification: Mentioning the specific tools (pandasql or sqlite3) guides the LLM to use the correct libraries and methods in the generated code.
-
Explicit Goals: By clearly listing the SQL operations you want to perform, you ensure the output meets your needs.
-
Expected Output: Stating that you want the results as pandas DataFrames steers the LLM to pay attention to the format of its final output.