Introduction to Numpy - π#
import numpy as np
Whereas panda helps sort and work with data. Numpy is for calculations.
Letβs create an array.
a = np.array([1, 2, 3, 4, 5, 6])
print(a)
[1 2 3 4 5 6]
b = np.array([[1, 2, 3],[4, 5, 6]])
print(b)
[[1 2 3]
[4 5 6]]
Two-dimensions.
c = np.array([[[1], [2], [3]],[[4], [5], [6]]])
print(c)
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
We can actually check this with the function ndim.
print(a.ndim)
print(b.ndim)
print(c.ndim)
1
2
3
or we can check the whole shapeβ¦
print(a.shape)
print(b.shape)
print(c.shape)
(6,)
(2, 3)
(2, 3, 1)
Your turn: Create a Rubikβs cube where each color is a number.
# code
We can also work with pandas and numpy together!
b = pd.DataFrame([[1, 2, 3],[4, 5, 6]])
b.columns = ['name1','name2','name3']
print(b)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[8], line 1
----> 1 b = pd.DataFrame([[1, 2, 3],[4, 5, 6]])
2 b.columns = ['name1','name2','name3']
3 print(b)
NameError: name 'pd' is not defined
b.to_numpy()
array([[1, 2, 3],
[4, 5, 6]])
or a specific column
b['name1'].to_numpy()
array([1, 4])
Now we can do something with the array.
d = b['name1'].to_numpy()
e = d + 10
print(e)
[11 14]
Letβs send it back to pandas.
b['name1'] = pd.array(e)
print(b)
name1 name2 name3
0 11 2 3
1 14 5 6
Your turn: using your rubrikβs cube
Add a constant to each value
Send to pandas
And add columns