ChatGPT for Data Wrangling Works

ChatGPT for Data Wrangling Works

How AI replaces majority of data scientists


6 min read

I find ChatGPT very good for data wrangling works that make up 80% of a data scientist's daily work.



I have a script to make a race plan given a GPX file and list of check points (example output of Lantau 70). The GPX file can be found from race organiser website.

Due to technology lagging, many organizer's today don't have check points embedded in the GPX file. Towards this end, my initial design is to allow user to input a list of check points in form of, 1) cummulative distance from start; 2) Or latitude and longitude tuple.

I got request from an ultra runner today to make a race plan for TNF 100 Mo Gan Shan. The official GPX file has check points ("waypoints", aka wpt) embedded. GPX file is an XML file in plain text format, so we can extract the information with any text editor and input in my planner spreadsheet.

Work with ChatGPT

GPX file overview

Following is the overview of a GPX file in case the reader did not see one before:

  1. Part 1 is the list of way points / check points (usually designated by organiser from a map)
  2. Part 2 is the list of track points (usually recorded from a sports watch/ or designed from a mapping software)
<?xml version="1.0" encoding="utf-8"?>
        creator="ovitalmap -"

<wpt lon="119.89916200" lat="30.60791600" >
        <name>起终点 郡安里度假酒店</name>
<wpt lon="119.89111824" lat="30.53001315" >
        <name>CP1 对河口水库</name>

... other way points omitted

                <trkpt lon="119.89916200" lat="30.60791600" >
                <trkpt lon="119.89952100" lat="30.60759700" >
                <trkpt lon="119.89949000" lat="30.60755500" >

... other track point ommitted

                <trkpt lon="119.89919300" lat="30.60792400" >
                <trkpt lon="119.89919300" lat="30.60792900" >

Instruction to ChatGPT

I give ChatGPT the following instructions:

read the following XML file, and convert to a tab separated table with name, lat, lon, ele


<wpt lon="119.89916200" lat="30.60791600" >
        <name>起终点 郡安里度假酒店</name>
<wpt lon="119.89111824" lat="30.53001315" >
        <name>CP1 对河口水库</name>
<wpt lon="119.87026124" lat="30.49539384" >
<wpt lon="119.80487134" lat="30.50543799" >
<wpt lon="119.78959181" lat="30.52616307" >
<wpt lon="119.80046350" lat="30.53920958" >
<wpt lon="119.80986392" lat="30.53757413" >
<wpt lon="119.80834456" lat="30.60588694" >
<wpt lon="119.82839453" lat="30.62405749" >
<wpt lon="119.86925290" lat="30.63074462" >


起终点 郡安里度假酒店30.60791600119.8991620053
CP1 对河口水库30.53001315119.8911182464

To convert this XML file to a tab-separated table with columns "name," "lat," "lon," and "ele," we simply need to extract the corresponding attributes and values for each "wpt" element and populate the table accordingly. For each "wpt" element, the "name" attribute corresponds to the first column, the "lat" attribute corresponds to the second column, the "lon" attribute corresponds to the third column, and the "ele" element corresponds to the fourth column.

Note that there is one "wpt" element with a missing "ele" element, which we denote as "N/A."

Notes of response

  • ChatGPT masters markdown very well, so the output table is well formatted. The table can be rendered in the Chat UI, or be copied into Google Sheets directly.
  • It even proactively pointed out that one way point has no elevation!


About data wrangling: (ref)

Data wrangling is the process of converting raw data into a usable form. It may also be called data munging or data remediation.

In contrast to the public's image of working on fancy models and visualizations everyday, many data scientists spend a majority of their time to transform data into usable format.

In our case today, I have two approaches in the past:

  1. Manually copy and paste from XML to Google Sheets -- If I deem this to be one off work, or when the data volume is really small, I go this route to speed up time-to-insights-lag.
  2. Write a parser script -- 10 lines of Python could do. The concept is easy but troubleshooting and handling corner cases may take some uncertain time. More importantly, we end up building many one-off scripts that are never re-used.

With ChatGPT, we can finish the data wrangling part in 5min. We can focus more of the core part, i.e. designing race plan in my case by tuning pace/ rest time/ etc.

In the past, either way of data wrangling is not ideal.

For method 1, I feel doing very fundamental work but the complexity & scale makes it very hard to delegate. One can finish those tasks with manual effort much faster than explaning the task to an assistant.

For method 2, I often enjoy writing concise Python scripts, just to keep my mind fresh (like people going to gym), but I know from bottom of my heart those scripts do not generalize well.

We can of course build a utility function for this task though:

import xml.etree.ElementTree as ET

def parse_xml_to_table(tree):
    # Find all "wpt" elements and extract the desired attributes and elements
    rows = []
    for wpt in tree.findall('wpt'):
        name = wpt.find('name').text
        lat = wpt.get('lat')
        lon = wpt.get('lon')
        ele_elem = wpt.find('ele')
        ele = ele_elem.text if ele_elem is not None else 'N/A'

        # Add the values to the list of rows
        rows.append([name, lat, lon, ele])

    # Return the resulting table (as a list of rows)
    return rows

def xml_to_table(filename):
    # Read in the XML file and parse it into an ElementTree object
    tree = ET.parse(filename)

    # Call the parse_xml_to_table function to extract the desired information
    table = parse_xml_to_table(tree)

    # Return the resulting table (as a list of rows)
    return table

Example usage:

table = xml_to_table('input.xml')

# Iterate over the rows of the resulting table and print them to the console
for row in table:

However, it is unlikely for me to do similar tasks next time. The nice utility function is like the T-Shirt at bottom of my wardrobe -- you know it is useful "some time", but the time never come.

BTW, above two Python snippets are written by ChatGPT!

One may wonder if we shall ask ChatGPT to write the Python snippet directly, instead of just parsing the table?

I know writing Python script feels pretty advanced, but I still prefer to ask for table output on first iteration, for two reasons:

  1. I don't have to run a Python interpreter / troubleshoot and get the output. I can proceed to my core business sooner.
  2. In our example, the missing elevation, aka "N/A", is well handled in Python script, primarily because we have a bad data point in the sample. This inspires us to come up with input/ output examples before instructing ChatGPT to design something for scale. This also mimics the real work of a data scientist. We often don't know what is in the dataset before we delve into it. So we prefer interactive environments, e.g. Jupyter Notebook, so that we can explore and fix things on the fly.

Did you find this article valuable?

Support HU, Pili by becoming a sponsor. Any amount is appreciated!