Creating notebooks for testing

Do another slide, loading real data, and contrast.

In [2]:
import numpy as np
import pandas as pd
In [3]:
# Create a dataframe with random numbers

# Make it consistent
np.random.seed(42)

df = pd.DataFrame(np.random.rand(3,5))
df
Out[3]:
0 1 2 3 4
0 0.374540 0.950714 0.731994 0.598658 0.156019
1 0.155995 0.058084 0.866176 0.601115 0.708073
2 0.020584 0.969910 0.832443 0.212339 0.181825
In [4]:
np.random.seed(42)

df = pd.DataFrame(np.random.rand(3,5), index=list("xyz"), columns=list("ABCDE"))
df
Out[4]:
A B C D E
x 0.374540 0.950714 0.731994 0.598658 0.156019
y 0.155995 0.058084 0.866176 0.601115 0.708073
z 0.020584 0.969910 0.832443 0.212339 0.181825
In [6]:
# Create a 3x5 list in sequence (not so good)
df_sequence = pd.DataFrame([np.arange(5) ,np.arange(5,10), np.arange(10,15)])
df_sequence
Out[6]:
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
In [7]:
# Rotating a dataframe with transpose
rotated = df_sequence.transpose()
rotated
Out[7]:
0 1 2
0 0 5 10
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
In [8]:
# Create a list in sequence -- better!
cols = ['x' + n for n in list('01234')]
indexes = ['y' + n for n in list('0123456789')]
df = pd.DataFrame(np.arange(1, 51).reshape(10,5), columns=cols, index=indexes)
df
Out[8]:
x0 x1 x2 x3 x4
y0 1 2 3 4 5
y1 6 7 8 9 10
y2 11 12 13 14 15
y3 16 17 18 19 20
y4 21 22 23 24 25
y5 26 27 28 29 30
y6 31 32 33 34 35
y7 36 37 38 39 40
y8 41 42 43 44 45
y9 46 47 48 49 50