When working with AI, it’s important to know how to import data sets, read through tables, and understand what the structure is.
Before we begin
My first Dataframe
Welcome to the “Product developers' guide to getting started with AI”. In this series, we’ll go over key concepts and run through examples using Pandas. First, we will cover setting up your development environment and learning how to inspect your data. Then, you’ll be ready to tackle the more exciting parts of AI throughout this series.
Before we begin
For the most part, Google Collab has everything already installed except the dataset, skip to My First Dataframe. However, if you want to run it locally then follow the next step. We’ll be using:
Downloading Prerequisites (Optional)
My First Dataframe
Click on New notebook
Next, we’ll begin by importing titanic.csv to create your first dataframe. Go to the file tab, and click on the file with the arrow to upload from your computer.
Click on the file with the arrow icon to import titanic.csv
Then import Pandas, Numpy, and use
to extract our CSV data into a dataframe.
At the beginning, import the libraries and file via code
Type the name of the dataframe to view it. Here we call it df, so in the next cell we type df. To run the cell use Shift+Enter or click the run icon at the left.
Display entire dataframe
Unlike a table, a dataframe has some extra data behind the scenes, called metadata. Metadata is used to organize its structure and can be viewed in Pandas by using the
method. Let’s say we wanted to know how many rows and columns contain non-empty values or how much storage the data takes up.
is a great method that product developers who have worked with SQL will find similar to the EXPLAIN command. It tells us valuable information about the storage space used, column information, number of rows, indices, and types. All while organizing it into an easy-to-read table.
Show all information about the dataframe
is a method best used to summarize the numerical data by calculating a quick mathematical summary and displaying the count, mean, min, max, standard deviation, and percentiles.
Default output of describe
This is by default equivalent to df.describe(include=[np.number])
Describe all numbers
By adding the object keyword,
looks for the unique, top, and frequency of the data for object data, such as strings and timestamps instead. Here, it selects the columns that have a data type of object from the output.
Describe all objects
Conversely, you may also use exclude instead of include to get the reverse outputs.
Describe everything that is not an object
Describe everything that is not a number
is an interesting method that is used to read metadata and select data. To get the metadata of a column, call it on a dataframe to get the index names.
Display all index names
There are two ways to select a column, using either the index position or index name. The index position can be found from the metadata of
on the left.
Access by index position
The index name can be found from the output of
Access by index name
But most of the time, especially when working with AI, you’ll have very large datasets and it may not be feasible or necessary to display everything. Dataframes have other features to view parts of the data, by using the
Time to use Python to chop down the data
Let’s take a look using indexing with the
To view the data on the first 5 rows, we use head(5)
Head refers to the start of the dataframe
Then, to view the data for the last 5 rows, we use tail(5)
Tail refers to the end of the dataframe
We can view multiple columns using
, specifying the row index found on the left of the dataframe, along with the names of the columns to view. Since our row index is unlabeled, we use integers to quickly access them. The ‘:’ command is to set a range of values, to include everything.
View each Name, Ticket, and Fare
, you can also use the index position with the
Name, Ticket, and Fare are 3, 8, and 9 respectively
Combining what we’ve learned, let’s answer common data analysis questions about the Titanic dataset that data scientists and marketing ask themselves every day.
How many people were aboard the Titanic when it sank?
we see that 889 people embarked on the ship
How much did the average passenger pay?
, the mean fare was $32
What was the standard deviation or “std” between ticket prices?
, std of the fare is $50
What was the highest cost for a ticket?
, the max fare is $512
Who was the first person to pay for a ticket?
on the name column, Mr. Owen Harris Braund
Who was the last person to pay for a ticket?
on the name column, Mr. Patrick Dooley
Who was the 100th person to purchase a ticket?
for row 99 of column, since position starts from 0, Mr. Sinai Kantor
That covers the
functions for reading metadata and
for viewing dataframes. Check back next week for our next guide, “Surfing through dataframes”, where we’ll be taking a look at how to search through our imported data by grouping, ordering, and rearranging the dataframe’s structure.