Lesson 32: Practice with Pandas

(c) 2016 Justin Bois. This work is licensed under a Creative Commons Attribution License CC-BY 4.0. All code contained herein is licensed under an MIT license.

This tutorial was generated from a Jupyter notebook. You can download the notebook here.

In [27]:
import numpy as np
import pandas as pd

Pandas can be a bit frustrating during your first experiences with it. In this lesson, we will practice using Pandas. The more and more you use it, the more distant the memory of life without it.

Practice 1: Mastering loc

We will again use the frog tongue adhesion data set. Your goal here is to extract certain entries out of the DataFrame. If it is not in your namespace, load in the DataFrame using pd.read_csv().

In [2]:
df = pd.read_csv('data/frog_tongue_adhesion.csv', comment='#')

a) Extract the impact time of all impacts that had an adhesive strength of magnitude greater than 2000 Pa.

b) Extract the impact force and adhesive force for all of Frog II's strikes.

c) Extract the adhesive force and the time the frog pulls on the target for juvenile frogs (Frogs III and IV).

Practice 2: The power of groupby()

The groupby() method is one of the most powerful methods of Pandas DataFrames. It works by splitting up a DataFrame based on some criterion. Once that happens, we can then apply a function to these split up DataFrame. Upon application of the function, we get a recombined DataFrame with the result.

This is best shown by example. The goal is to compute the mean impact force of each frog. First, do it the "long way."

  1. Extract all of Frog I's impact forces and compute the mean.
  2. Do the same for the other three frogs.
  3. Write a for loop to do this and return a NumPy array with the four mean impact forces.

Now, unfortunately, you don't get a DataFrame out of this. You only get a NumPy array. But if you use groupby(), you do. I'll show how it works by example.

In [28]:
# We only want ID's and impact forces, so slice those out
df_impf = df.loc[:, ['ID', 'impact force (mN)']]

# Make a GroupBy object
grouped = df_impf.groupby('ID')

# Apply the np.mean function to the grouped object
df_mean_impf = grouped.apply(np.mean)

# Look at the new DataFrame
df_mean_impf
Out[28]:
impact force (mN)
ID
I 1530.20
II 707.35
III 550.10
IV 419.10

Sweet! Look at that! We have a DataFrame with the results. We can pull the mean impact force for a frog of interest using loc.

In [30]:
df_mean_impf.loc['III', :]
Out[30]:
impact force (mN)    550.1
Name: III, dtype: float64

Now, what is we want more information, like both the mean and the median? We can apply multiple functions to a GroupBy object using the agg() method. The argument of this method is a list of functions you want to apply.

In [34]:
grouped.agg([np.mean, np.median])
Out[34]:
impact force (mN)
mean median
ID
I 1530.20 1550.5
II 707.35 573.0
III 550.10 544.0
IV 419.10 460.5

Now, let's practice with groupby().

a) Compute standard deviation of the impact forces for each frog.

b) Write a function, coeff_of_var(data), which computes the coefficient of variation of a data set. This is the standard deviation divided by the absolute value of the mean.

c) Compute coefficient of variation of the impact forces and adhesive forces for each frog.

d) And now, finally.... Compute a DataFrame that has the mean, median, standard deviation, and coefficient of variation of the impact forces and adhesive forces for each frog.