June 3 2024
June 3 2024
June 3 2024
June 3, 2024
Creating an array
[3]: l = [10,20,30,40,50,60]
numbers = np.array(l)
print(numbers)
print(type(numbers))
[10 20 30 40 50 60]
<class 'numpy.ndarray'>
Creating 2d Array
[7]: l = [
[10,20,30],
[40,50,60]
]
TwoD = np.array(l)
print(TwoD)
print(type(TwoD))
print(TwoD.ndim) # ndim ek arttribute hai method nahi.
# ndim attribute tell us about number of dimension present in an array
[[10 20 30]
[40 50 60]]
<class 'numpy.ndarray'>
2
Creating a 3D array
[11]: l = [
[
[1,2,3],
1
[4,5,6]
],
[
[7,8,9],
[10,11,12]
],
]
# 3darray = np.array(l)
ThreeDarray = np.array(l)
print(ThreeDarray)
print(type(ThreeDarray))
print(ThreeDarray.ndim) # print the number of dimension in an array
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
<class 'numpy.ndarray'>
3
Creating an array using random module
[36]: import numpy as np
from numpy import random
[36]: array([11, 17, 18, 18, 15, 11, 10, 14, 14, 14])
[37]: arr2 = random.randint(10,20, size = (10, 3)) # iska matalab hai ki 10 list␣
↪chahiye aur hr ek list m 3 elements ho
arr2
2
[38]: arr3 = random.randint(10,20, size=(5, 3, 2))
arr3
[[15, 11],
[14, 15],
[10, 14]],
[[14, 13],
[16, 12],
[13, 15]],
[[10, 12],
[17, 16],
[10, 19]],
[[10, 14],
[15, 13],
[13, 16]]])
[43]: l = [10,20,30,40,50,60]
numbers = np.array(l)
print(numbers)
print(type(numbers))
print(numbers[0]) # indexing starts from 0
print(numbers[2])
[10 20 30 40 50 60]
<class 'numpy.ndarray'>
10
30
100
[ 10 20 30 100 50 60]
3
[46]: # sorting the array
numbers.sort()
print(numbers)
[ 10 20 30 50 60 100]
0.1 Pandas
[ ]: # 1. Series - one dimensional (same as list)
# 2. Dataframe - 2 dimensional
prajwal = pd.Series(ls)
print(prajwal)
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 9
8 10
9 8
dtype: int64
Creating a series using an array
[51]: import numpy as np
from numpy import random
import pandas as pd
0 5
1 6
2 8
3 6
4 8
dtype: int32
4
0.1.1 adding custom index
[68]: ls = [15,20,17,56,18]
ser = pd.Series(ls, index=['A', "B", "C", 'D', 'E'])
print(ser)
# the size (length) of the index must be equals to the length of series
A 15
B 20
C 17
D 56
E 18
dtype: int64
20
56
[77]: print(ser)
print(ser['B':'D']) # include all cusom indexes
print(ser[1:3]) # taking default indexing ( 0 to n) #3 ko include nahi karega
# defaul indexing me n-1 element tak print karega
A 15
B 20
C 17
D 56
E 18
dtype: int64
B 20
C 17
D 56
dtype: int64
B 20
C 17
dtype: int64
[80]: ls = [15,20,17,56,18]
s = pd.Series(ls, index=['A', "B", "C", 'D', 'E'])
print(s)
s['C'] = 71 # C wale index ki value change kar do bas
print(s)
5
s[4] = 81 # 4th index ki value ko change kar dega
print(s)
# element ko modify kiya jaa sakta hai-> custom or default dono indexing ka use␣
↪karke
A 15
B 20
C 17
D 56
E 18
dtype: int64
A 15
B 20
C 71
D 56
E 18
dtype: int64
A 15
B 20
C 71
D 56
E 81
dtype: int64
printing the series in reverse order
[81]: s[::-1] #
[81]: E 81
D 56
C 71
B 20
A 15
dtype: int64
A 15
B 20
C 71
D 56
E 81
dtype: int64
E 81
D 56
6
C 71
B 20
A 15
dtype: int64
sorting the series by values
[90]: s.sort_values() # ascending order
[90]: A 15
B 20
D 56
C 71
E 81
dtype: int64
[89]: E 81
C 71
D 56
B 20
A 15
dtype: int64