Cheatsheet#
Patterns and examples of how to use common tips in class
How to use brackets#
symbol |
use |
|
indexing item val from an object; |
|
slicing elemtns val to val2-1 from a listlike object |
|
creating a list consisting of |
|
function calls |
|
defining a tuple of |
|
defining a set of |
|
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
A | B | |
---|---|---|
0 | 1 | 0 |
1 | 5 | 4 |
2 | 1 | 4 |
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]
5
df.iloc[0][1]
0