Hands-on Python pandas.DataFrame

A tutorial introduction to Python pandas.DataFrame

Leah Erb
Women Data Greenhorns
9 min readAug 2, 2018

--

Image Courtesy : Combination built with pandas logo owned by pandas.pydata.org and waving pandas on Amazon.com

One of the first concepts we learn as Python programmers is about built-in data structures, such as the list and the dictionary.

Once you understand these basic structures, it’s time to add pandas.DataFrame to your set of Python programming essentials.

Why pandas.DataFrame?

Because pandas.DataFrame is one of the most used Python data structures, and for good reason. It saves time and anguish, and if you’re going to be reading or collaborating or borrowing other programmers’ code, you are absolutely going to see DataFrames sooner than later.

About this tutorial

A great way to learn is by doing.

This tutorial includes exercises designed to gently introduce you to the world of pandas.DataFrame.

The tutorial uses a subset of data from the popular Titanic data set (if you’re a student of data science, you’ll be seeing the Titanic data set again).

Feel free to use your own data instead, simply save a spreadsheet of your choice to CSV format. Otherwise, …

Download the Titanic CSV file into your Python 3 programming environment:

pandas and DataFrame at a glance

pandas is a powerful Python package widely used for data analysis. It simplifies tasks for loading, analyzing and manipulating data that would otherwise require way too many lines of Python code.

DataFrame is a data structure (just like list and dictionary are data structures), included in the pandas package.

According to the Pandas website:

“DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object. Like Series, DataFrame accepts many different kinds of input …

Example view of a DataFrame

As you can see, the DataFrame structure is much like a spreadsheet or database table:

pandas.dataframe populated with Titanic data

Let’s get started with the tutorial …

Import data into a DataFrame

Pandas has many reader functions that import an external data source (like a CSV or HTML text file) and return a DataFrame.

Use read_csv() to load this tutorial’s Titanic data

A successful read_csv() has no output. Let’s see what it created …

View a DataFrame

3 functions & methods for creating a birds-eye view of a DataFrameare: shape, head(), and describe().

How many rows and columns?

891 rows and 6 columns.

What does the data look like?

head() displays the first 5 records. To see fewer or more records, pass an integer, example: head(10).

Notice: the left side of the DataFrame table lists sequential integers 0, 1, …, 4. This is the ‘index’, or ‘row label’. We’ll discuss indexes a little later on in this tutorial.

Are there statistics on the data?

describe() outputs a summary of descriptive statistics. Notices the results are a DataFrame itself.

In this example, the count row contains the count of non-null values in each numeric column of the titanic_df. The column Age has 714 non-null values, which means the rest of the 891 records have Age = null (no value). As an analyst, you may be concerned with null values.

Click here for more descriptive statistics and computation methods, such as: count(), max(), min(), mean(), std(), sum(), dtypes()

Click here for more views of the underlying structure and meta data.

Now that you have an idea of what a DataFrame looks like, let’s make some from scratch …

Create a DataFrame manually

Syntax

pandas.DataFrame(data, index, columns, dtype, copy)

Create a simple DataFrame

Define the first parameter, data, as a simple list [‘A’, ‘B’, ‘C’]. Accept defaults for all other parameters and see what happens:

Notice the indexes (or, row labels), on the left. They default to a range sequence starting with 0.

Create a DataFrame with a list array

Practice using parameters by passing both index and column labels, and force the data to be of type float:

Nice.

Create a DataFrame with a dictionary of lists

Perfect.

Copy a data structure to a DataFrame

Another way of creating a DataFrame is to copy from an existing data structure such as: a list of dictionaries, narrays/lists, Series (a pandas structure), and a list of Series.

Copy from list of dictionaries

Use the method from_dict().

For details on from_dict(), from_records() and other methods for copying data to and from a DataFrame, click here.

Select specific columns

There are three methods of selecting columns from a DataFrame, and they all return the same results:

  • dot notation
    dataframe.column_name
  • square braces with the name of the column as a string dataframe[‘column_name’]
  • numeric indexing and the iloc selector
    dataframe.iloc[:, <column_number>]

(include head() method to limit rows returned)

I believe the square brackets version is the cleanest and easiest to read. Also, less error-prone than iloc if columns are re-ordered, added or removed from the DataFrame later. iloc’s slicing syntax is handy, however, when selecting a series of columns.

Select multiple columns

Include multiple column names as a list.

Select specific rows

Select the second set of 5 rows by slicing inside of brackets.

Notice how the index (left side) is the index for each specific record in the DataFrame, not for the selection result’s table row.

These two commands are the same, each returning the first 5 rows of a DataFrametitanic_df.head(5) = titanic_df[0:5].

Select specific columns and rows at the same time

Columns get listed first, then rows are sliced.

Filter records based on conditions

Who on the Titanic was older than 65?

How many people survived? (0 = no, 1 = yes)

What was the survival rate, in percentages?

What percentage of seniors survived?

Not a good survival rate folks over 65 years old.

Sort by column values

Who were the 10 youngest?

How about the 10 oldest?

For more on slicing and selecting data, click here.

Group by columns

How many survived and died, grouped by females and males?

Use the df.groupby() method.

Change data

Update ‘Survived’, converting a Boolean to a String.

Remember, with Booleans, 0=False and 1=True.

Use the pandas.DataFrame.replace() method. Create a boolean dictionary (boolean_d) first.

Indexing

By default, a DataFrame’s rows are indexed with sequential integers starting with 0. What if we want to index on a column that already exists, instead? For example, on the ‘PassengerId’ column?

There are a couple ways to set an index: convert an existing column to an index, or set the index when the DataFrame is built.

Convert column to index

Use set_index().

Index DataFrame upon creation

Now let’s re-import our test data to create titanic_df using read_csv again, this time explicitly assigning an index instead of accepting the default range index. Use the index_col parameter.

Indexes are not columns

An important thing to note here: now that ‘PassengerId’ is an index, it is no longer a column in the DataFrame. This is demonstrated with the shape function, which shows there are now 5 columns instead of previous 6:

Also, an index is not referenced like a column. For example, if you try to reference ‘PassengerId’ as you would a column, you’ll get an error:

Convert index to a column

Change your mind about indexing that column? No worries. Just set it back using reset_index():

Conclusion (with a teaser)

This tutorial reveals only the very tip of the pandas.DataFrame iceberg.

We haven’t even touched on doing math, using apply and applymap to apply functions across a DataFrame, pivoting tables, multi-indexing, using crosstab to validate a hypothesis, merging, iterating over rows, and so much more.

I encourage you to keep exploring the pandas.DataFrame, and the pandas library in general.

Before you go, here’s one last teaser …

Plotting pandas.DataFrame

One of the best things about pandas and theDataFrame structure is how easy it makes doing data analysis, especially when coupled with the matplotlib library. matplotlib is a whole new subject, so here’s just a simple example to wet your appetite.

Try it

Happy Python-ing!!!

--

--