Skip to article frontmatterSkip to article content

Cheatsheet

Patterns and examples of how to use common tips in class

How to use brackets

symbol

use

[val]

indexing item val from an object; val is int for iterables, or any for mapping

[val : val2]

slicing elemtns val to val2-1 from a listlike object

[ item1,item2 ]

creating a list consisting of item1 and item2

(param)

function calls

(item1,item2)

defining a tuple of item1 and item2

{item1,item2}

defining a set of item1 and item2

{key:val1, key2: val2}

defining a dictionary where key1 indexes to val2

Axes

First build a small dataset that’s just enough to display

data = [[1,0],[5,4],[1,4]]
df = pd.DataFrame(data = data,
  columns = ['A','B'])

df
Loading...

This data frame is originally 3 rows, 2 columns. So summing across rows will give us a Series of length 3 (one per row) and long columns will give length 2, (one per column). Setting up our toy dataset to not be a square was important so that we can use it to check which way is which.

df.sum(axis=0)
A 7 B 8 dtype: int64
df.sum(axis=1)
0 1 1 9 2 5 dtype: int64
df.apply(sum,axis=0)
A 7 B 8 dtype: int64
df.apply(sum,axis=1)
0 1 1 9 2 5 dtype: int64

Indexing

df['A'][1]
np.int64(5)
df.iloc[0][1]
/tmp/ipykernel_2503/2309508539.py:1: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`
  df.iloc[0][1]
np.int64(0)

Markdown Headings

To create a heading in a notebook, make a a line that starts with a #

# This is a main title
## This is a sub heading
### sub sub heading
## sub heading