Contents
- NumPy
- Pundas
- Xarray
- SciPy
- Matplotlib
Reference
Use ” https://colab.research.google.com ” for Editing.
Numpy
a-range, re-shape
import numpy as np
a = np.arange(30)
print(a)
b = a.reshape(2,5,3)
print(b)
////output
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29]
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]
[12 13 14]]
[[15 16 17]
[18 19 20]
[21 22 23]
[24 25 26]
[27 28 29]]]
array
import numpy as np
x = np.array([[0,1,2],[3,4,5],[6,7,8],[9,10,11]])
print("Input array is:")
print(x)
print("\n")
rows = np.array([[0,0],[3,3]])
colomns = np.array([[0,2],[0,2]])
print('Rows \n{}'.format(rows))
print('Colomns \n{}\n'.format(colomns))
y = x[rows,colomns]
print('Output array is:\n{}'.format(y))
z = x[1:4,1:3]
print('\nUsing slice operation\n{}'.format(z))
m = x[1:4,[0,1]]
print('\nUsing advanced indexing\n{}\n'.format(m))
print('\nBoolean indexing\n{}'.format(x[x > 5]))
////output
Input array is:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
Rows
[[0 0]
[3 3]]
Colomns
[[0 2]
[0 2]]
Output array is:
[[ 0 2]
[ 9 11]]
Using slice operation
[[ 4 5]
[ 7 8]
[10 11]]
Using advanced indexing
[[ 3 4]
[ 6 7]
[ 9 10]]
Boolean indexing
[ 6 7 8 9 10 11]
array addition
import numpy as np
a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])
b = np.array([1.0,2.0,3.0])
print('First array:\n{}\n'.format(a))
print('Second array:\n{}\n'.format(b))
print('First Array + Second Array\n{}\n'.format(a+b))
///output
First array:
[[ 0. 0. 0.]
[10. 10. 10.]
[20. 20. 20.]
[30. 30. 30.]]
Second array:
[1. 2. 3.]
First Array + Second Array
[[ 1. 2. 3.]
[11. 12. 13.]
[21. 22. 23.]
[31. 32. 33.]]
Array Transpose
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print( 'Original array is:')
print( a )
print ('\n' )
print ('Transpose of the original array is:' )
b = a.T
print (b )
print ('\n' )
print( 'Modified array is:' )
for x in np.nditer(b):
print (x)
/////Output
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Transpose of the original array is:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
Modified array is:
0
5
10
15
20
25
30
35
40
45
50
55
Pandas
Series
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve the first three element
print(s[:3])
print(s['e'])
////Output
a 1
b 2
c 3
dtype: int64
5
DataFrame
import pandas as pd
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
print(df)
////Output
Name Age
rank1 Tom 28
rank2 Jack 34
rank3 Steve 29
rank4 Ricky 42