June 3 2024

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

june-3-2024

June 3, 2024

Creating a numpy nDarray


[1]: import numpy as np
import pandas as pd
from numpy import random

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

# arr = random.randint(minimum value, maximum value, size = (no of elements) )


arr = random.randint(10, 20, size=10) # 10 bhi include hoga
arr

[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

[37]: array([[10, 14, 11],


[11, 19, 11],
[10, 15, 16],
[17, 18, 11],
[16, 18, 14],
[10, 17, 10],
[19, 14, 13],
[10, 15, 11],
[17, 15, 19],
[18, 17, 15]])

2
[38]: arr3 = random.randint(10,20, size=(5, 3, 2))
arr3

[38]: array([[[11, 16],


[10, 19],
[15, 17]],

[[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])

# jese ham list slicing karte hai wse hi karna hai


numbers[3] = 100
print(numbers[3])
print(numbers)

[10 20 30 40 50 60]
<class 'numpy.ndarray'>
10
30
100
[ 10 20 30 100 50 60]

[45]: # printing the reversed array


numbers[::-1]

[45]: array([ 60, 50, 100, 30, 20, 10])

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

[48]: import pandas as pd


import numpy as np

[49]: # creating a series from list


ls = [1,2,3,4,5,6,7,9,10,8]

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

[62]: arr = random.randint(1, 9, 5)


ishika = pd.Series(arr)
print(ishika)

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

0.1.2 Accessing the element of the series object

[73]: print(ser['B']) # passing the custom index


print(ser['D'])

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

sorting the series


[86]: print(s.sort_index()) # ascending order
print(s.sort_index(ascending=False)) # descending

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]: s.sort_values(ascending=False) # descending order

[89]: E 81
C 71
D 56
B 20
A 15
dtype: int64

You might also like