Pandas cheat sheet

Preliminaries

Before doing anything else, go through the 10-minute intro tutorial.

For a fuller treatment, dip your toes into chapters 7, 11, 12, 13, 16, 17, and 18 of Crystal Ball vol One. I try to be as terse as I can in there without omitting anything important, so I think any time you invest in these chapters will be well rewarded.

Import the package

import pandas as pd” is the standard import, at the top of every file. (If this doesn’t work for you, exit the interpreter, make sure the correct virtual environment is activated, and then type “pip install pandas” at the command line.)

Tweaking printing

By default, Pandas doesn’t print out as much of its DataFrames as I like. So I set these two options at the top of my file:

pd.set_option("display.max.columns",80)
pd.set_option("display.width",240)

(There’s nothing magic about those particular values; I just want it to be “big enough.”)

About DataFrames

The DataFrame is the lingua franca of modern data analysis. It is a spreadsheet-y looking structure with rows (normally displayed left-to-right) and columns (up-to-down), but rows and columns are not interchangeable. Among other things, DataFrame rows are normally heterogeneous (they have many different data types within them) whereas columns are always homogeneous (all one data type).

Suppose you have successfully loaded a DataFrame called df. You can see the names of all its columns typing “df.columns”, and you can see the data type of each column by typing “df.dtypes”. Note that a type of “object” means “string data.”

Each column of a DataFrame is a smaller, one-dimensional Pandas object called a Series. If your df DataFrame has a column Weapon, you can access that column alone by typing either df['Weapon'] or df.Weapon. The latter is shorter, but is only possible when you have no spaces or other funky characters in the column name. It’s also only possible when the column you want already exists; if you want to create a new column – say, called Armor – you’ll have to use the first form (df['Armor'] = ...).

Possible connection

Btw, if you’ve taken CPSC 350, you’re familiar with various ways to slice, dice, join, view, filter, and update relational database tables. Those instincts will serve you very well here, since a DataFrame is essentially a table, with extra functionality available to you in Python.

Semi-automated ways to produce a DataFrame

Use pd.read_csv(filename) to read a comma-separated, or tab-separated (with sep="\t" argument) text file. You can also use pd.read_html() to screen-scrape HTML tables, pd.read_json() to import JSON files, pd.read_sql() to read from SQL databases, etc. Each of these operations requires its own documentation and are not generally compatible with each other. You must read the docs!

Some useful things to do on a DataFrame

Say you have a DataFrame named df. You can:

Some useful things to do on a Series (DataFrame column)