0% found this document useful (0 votes)
13 views60 pages

Numpy Pandas Matplotlib Tutorial - Jupyter Notebook

The document provides a comprehensive overview of using the NumPy library in Python, including how to create arrays, define data types, and manipulate array dimensions. It also covers functions for generating random numbers and arrays, as well as visualizing data with matplotlib. Key functions and their syntax are highlighted throughout the document.

Uploaded by

banen73504
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views60 pages

Numpy Pandas Matplotlib Tutorial - Jupyter Notebook

The document provides a comprehensive overview of using the NumPy library in Python, including how to create arrays, define data types, and manipulate array dimensions. It also covers functions for generating random numbers and arrays, as well as visualizing data with matplotlib. Key functions and their syntax are highlighted throughout the document.

Uploaded by

banen73504
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Import a library

In [1]: 1 import numpy as np

[Link]
([Link]

In [2]: 1 a=[Link]([1,2,3,4,5])
2 b=[Link]([1,2,3,4,5])
3 print(a)
4 print(b)

[1 2 3 4 5]
[1 2 3 4 5]

In [3]: 1 type(a)# tells which class its belongs

Out[3]: [Link]

In [4]: 1 type(b)

Out[4]: [Link]

if you want to define a data type of an array then We use [Link](arr,dtype= " define variable
type")

In [5]: 1 a=[Link]([1,2,3,4,5])
2 [Link]# tells a data type of an elements stored in an array

Out[5]: dtype('int32')

In [6]: 1 a=[Link]([1,2,3,4,5],dtype="float")
2 print(a)
3 [Link]

[1. 2. 3. 4. 5.]

Out[6]: dtype('float64')

syntax:

[Link](object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)

object: array like


dtype: data type

copy :f true (default), then the object is copied. Otherwise, a copy will only be made if array
returns a copy, if obj is a nested sequence, or if a copy is needed to satisfy any of the other
requirements (dtype, order, etc.).

subok:bool, optional: If True, then sub-classes will be passed-through, otherwise the returned
array will be forced to be a base-class array (default).

ndmin (int, optional): Specifies the minimum number of dimensions that the resulting array
should have. Ones will be prepended to the shape as needed to meet this requirement.

In [7]: 1 [Link]([1, 2, 3])

Out[7]: array([1, 2, 3])

In [8]: 1 [Link]([1, 2, 3.0])

Out[8]: array([1., 2., 3.])

In [9]: 1 a=[Link]([[1, 2], [3, 4]])


2 [Link] # arr_name.ndim tells the dimension of a array

Out[9]: 2

In [10]: 1 [Link]([1, 2, 3], ndmin=2)

Out[10]: array([[1, 2, 3]])

In [11]: 1 [Link]([1, 2, 3], dtype=complex)

Out[11]: array([1.+0.j, 2.+0.j, 3.+0.j])

In [12]: 1 #Data-type consisting of more than one element


2 x = [Link]([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
3 x['a']

Out[12]: array([1, 3])

In [13]: 1 #Creating an array from sub-classes:


2 [Link]([Link]('1 2; 3 4'))

Out[13]: array([[1, 2],


[3, 4]])
In [14]: 1 [Link]([Link]('1 2; 3 4'), subok=True)

Out[14]: matrix([[1, 2],


[3, 4]])

In [15]: 1 b=[Link]([[[1,2,3,4],[1,23,45,7]],[[1,2,3,45],[25,67,89,46]]])
2 [Link]

Out[15]: 3

In [16]: 1 print([Link][0],[Link][1],[Link][2])

2 2 4

In [17]: 1 c=[Link]([[[1,2,3],[12,23,34],[2,3,4,]],[[1,2,3],[2,3,5],[23,34,45]]])

In [18]: 1 [Link]

Out[18]: (2, 3, 3)

In [19]: 1 [Link][0]

Out[19]: 2

In [20]: 1 [Link]

Out[20]: 3

In [21]: 1 c[1,0,2] #acessing an element

Out[21]: 3

In [22]: 1 A=[Link]([2])
2 [Link]

Out[22]: 2

In [23]: 1 B=[Link](2)# 1-d array


2 [Link]

Out[23]: 0

In [24]: 1 [Link]
2 # size of an array means total no of elements in an array

Out[24]: 18
In [25]: 1 [Link]
2 # total no of bytes ARE taking inside

Out[25]: 72

Numpy([Link],reshape,random)

[Link]([start, ]stop, [step, ]dtype=None, *, like=None

Return evenly spaced values within a given interval.

start:(integer or real, optional) Start of interval. The interval includes this value. The default start
value is 0

stop:(integer or real)

End of interval. The interval does not include this value, except in some cases where step is
not an integer and floating point round-off affects the length of out.

In [26]: 1 [Link](-3, 3, 2, dtype=int)

Out[26]: array([-3, -1, 1])

In [27]: 1 [Link](3,7,2)

Out[27]: array([3, 5])

In [28]: 1 A=[Link](100)
2 A

Out[28]: array([ 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, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])

In [29]: 1 A=[Link](20,100,3)# for i in range(20,100,3)


2 A

Out[29]: array([20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68,
71, 74, 77, 80, 83, 86, 89, 92, 95, 98])
In [30]: 1 print(list(range(1,10)))

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

[Link](x)

Randomly permute a sequence, or return a permuted range.

x=int or array

In [31]: 1 [Link]([Link](10))

Out[31]: array([2, 8, 0, 4, 9, 3, 1, 7, 5, 6])

[Link]

[Link](low, high=None, size=None, dtype=’l’)

Return random integers from low (inclusive) to high (exclusive).

Return random integers from the “discrete uniform” distribution of the specified dtype in the
“half-open” interval [low, high). If high is None (the default), then results are from [0, low).

low:

int or array-like of ints

Lowest (signed) integers to be drawn from the distribution (unless high=None, in which case
this parameter is one above the highest such integer).

high:int or array-like of ints, optional

If provided, one above the largest (signed) integer to be drawn from the distribution (see above
for behavior if high=None). If array-like, must contain integer values

size : int or tuple of ints, optional

Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is
None, in which case a single value is returned.

In [32]: 1 a=[Link](20,30)
2 a

Out[32]: 25
In [33]: 1 type(a)

Out[33]: int

In [34]: 1 [Link](2, size=10)

Out[34]: array([1, 0, 1, 1, 1, 0, 1, 0, 1, 1])

Generate a 2 x 4 array of ints between 0 and 4, inclusive

In [35]: 1 [Link](5, size=(2, 4))

Out[35]: array([[3, 4, 1, 3],


[3, 1, 1, 0]])

Generate a 1 x 3 array with 3 different upper bounds

In [36]: 1 [Link](1, [3, 5, 10])

Out[36]: array([1, 2, 9])

Generate a 1 by 3 array with 3 different lower bounds

In [37]: 1 [Link]([1, 5, 7], 10)

Out[37]: array([2, 6, 8])

Generate a 2 by 4 array using broadcasting with dtype of uint8

In [38]: 1 a=[Link]([1, 3, 5, 7], [[10], [20]], dtype=np.uint8)


2 a

Out[38]: array([[ 6, 3, 7, 9],


[14, 15, 13, 18]], dtype=uint8)

In [39]: 1 [Link]

Out[39]: dtype('uint8')

[Link](d0, d1, ..., dn):

Random values in a given shape.


In [40]: 1 b=[Link](67)
2 b

Out[40]: array([0.14737294, 0.77371117, 0.5574604 , 0.00448748, 0.28159868,


0.47386222, 0.28394702, 0.31419927, 0.35668931, 0.50448741,
0.85607768, 0.23687189, 0.4017623 , 0.70858288, 0.42794168,
0.05585011, 0.5957312 , 0.66105376, 0.58206088, 0.30719165,
0.16720044, 0.60387885, 0.55678567, 0.3525468 , 0.90701575,
0.51464461, 0.79753509, 0.31189649, 0.89718017, 0.36603418,
0.2773117 , 0.06701191, 0.40472098, 0.08333053, 0.91744506,
0.87633666, 0.00852464, 0.66280833, 0.45932126, 0.73961671,
0.75632894, 0.05593656, 0.50505072, 0.01360631, 0.90981372,
0.81487967, 0.24647694, 0.81520211, 0.94973456, 0.49737342,
0.26575474, 0.86012077, 0.77683034, 0.73228339, 0.67550809,
0.11080958, 0.76353756, 0.80671074, 0.85001182, 0.48665839,
0.24292104, 0.36590507, 0.2527439 , 0.32060075, 0.15042959,
0.40087927, 0.35605418])

In [41]: 1 import [Link] as plt


2 [Link](b,bins=5)

Out[41]: (array([11., 17., 13., 10., 16.]),


array([0.00448748, 0.1935369 , 0.38258631, 0.57163573, 0.76068514,
0.94973456]),
<BarContainer object of 5 artists>)

[Link]

In [42]: 1 b=[Link](100000)
In [43]: 1 [Link](b,bins=200)
, , , , ,
3.85247153, 3.89550465, 3.93853777, 3.98157088, 4.024604 ,
4.06763712, 4.11067024, 4.15370336, 4.19673648, 4.23976959,
4.28280271]),
<BarContainer object of 200 artists>)

Return a sample (or samples) from the “standard normal” distribution.

In [44]: 1 c=[Link](2,3)
2 c

Out[44]: array([[0.85987082, 0.32925252, 0.84359651],


[0.76553025, 0.65408193, 0.46987802]])

In [45]: 1 [Link]

Out[45]: 2

In [46]: 1 f=[Link](2,3,4,2)
In [47]: 1 f

Out[47]: array([[[[0.63290939, 0.11873574],


[0.18041621, 0.69993445],
[0.73451659, 0.68679952],
[0.26361088, 0.24625917]],

[[0.9918514 , 0.59811509],
[0.18041814, 0.16472693],
[0.61518047, 0.07070645],
[0.37736273, 0.80914649]],

[[0.6840498 , 0.08701545],
[0.97204267, 0.61803372],
[0.83348168, 0.03573135],
[0.72095525, 0.87603509]]],

[[[0.16955355, 0.30847154],
[0.80869495, 0.99278668],
[0.39943373, 0.44563388],
[0.01930691, 0.25126703]],

[[0.93256054, 0.57091998],
[0.64571133, 0.42720771],
[0.24318278, 0.36551387],
[0.56897703, 0.33657849]],

[[0.10234947, 0.1445751 ],
[0.03456438, 0.22017005],
[0.67139146, 0.27889377],
[0.49990398, 0.87893307]]]])

In [48]: 1 [Link]

Out[48]: 4

[Link]([start,] stop[, step,][, dtype])

start : [optional] start of interval range. By default start = 0

stop : end of interval range

step : [optional] step size of interval. By default step size = 1,

For any output out, this is the distance between two adjacent values, out[i+1] - out[i].

dtype : type of output array


In [49]: 1 c=[Link](100).reshape(4,25)

In [50]: 1 c

Out[50]: array([[ 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, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
66, 67, 68, 69, 70, 71, 72, 73, 74],
[75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99]])

In [51]: 1 [Link]

Out[51]: (4, 25)

In [52]: 1 c=[Link](100).reshape(4,5,5)
2 c

Out[52]: array([[[ 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],


[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39],
[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49]],

[[50, 51, 52, 53, 54],


[55, 56, 57, 58, 59],
[60, 61, 62, 63, 64],
[65, 66, 67, 68, 69],
[70, 71, 72, 73, 74]],

[[75, 76, 77, 78, 79],


[80, 81, 82, 83, 84],
[85, 86, 87, 88, 89],
[90, 91, 92, 93, 94],
[95, 96, 97, 98, 99]]])

In [53]: 1 [Link]

Out[53]: (4, 5, 5)

[Link](array, shape, order = 'C')

array : [array_like]Input array


shape : [int or tuples of int] e.g. if we are arranging an array with 10 elements then shaping it
like [Link](4, 8) is wrong; we can do [Link](2, 5) or (5, 2)

order : [C-contiguous, F-contiguous, A-contiguous; optional]


C-contiguous order in memory(last index varies the fastest) C order means that operating row-
rise on the array will be slightly quicker FORTRAN-contiguous order in memory (first index
varies the fastest). F order means that column-wise operations will be faster. ‘A’ means to read
/ write the elements in Fortran-like index order if, array is Fortran contiguous in memory, C-like
order otherwise

In [54]: 1 import numpy as geek


2
3 # array = [Link](8)
4 # The 'numpy' module has no attribute 'arrange'
5 array1 = [Link](8)
6 print("Original array : \n", array1)
7
8 # shape array with 2 rows and 4 columns
9 array2 = [Link](8).reshape(2, 4)
10 print("\narray reshaped with 2 rows and 4 columns : \n",
11 array2)
12
13 # shape array with 4 rows and 2 columns
14 array3 = [Link](8).reshape(4, 2)
15 print("\narray reshaped with 4 rows and 2 columns : \n",
16 array3)
17
18 # Constructs 3D array
19 array4 = [Link](8).reshape(2, 2, 2)
20 print("\nOriginal array reshaped to 3D : \n",
21 array4)
22 ​

Original array :
[0 1 2 3 4 5 6 7]

array reshaped with 2 rows and 4 columns :


[[0 1 2 3]
[4 5 6 7]]

array reshaped with 4 rows and 2 columns :


[[0 1]
[2 3]
[4 5]
[6 7]]

Original array reshaped to 3D :


[[[0 1]
[2 3]]

[[4 5]
[6 7]]]
[Link](shape, dtype = None, order = 'C')

function returns a new array of given shape and type, with zeros.

shape : integer or sequence of integers

order : C_contiguous or F_contiguous C-contiguous order in memory(last index varies the


fastest) C order means that operating row-rise on the array will be slightly quicker FORTRAN-
contiguous order in memory (first index varies the fastest). F order means that column-wise
operations will be faster.

In [55]: 1 import numpy as geek


2
3 b = [Link](2, dtype = int)
4 print("Matrix b : \n", b)
5
6 a = [Link]([2, 2], dtype = int)
7 print("\nMatrix a : \n", a)
8
9 c = [Link]([3, 3])
10 print("\nMatrix c : \n", c)

Matrix b :
[0 0]

Matrix a :
[[0 0]
[0 0]]

Matrix c :
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]

[Link](shape, dtype = None, order = 'C')

function returns a new array of given shape and type, with ones.

shape : integer or sequence of integers

order : C_contiguous or F_contiguous C-contiguous order in memory(last index varies the


fastest) C order means that operating row-rise on the array will be slightly quicker FORTRAN-
contiguous order in memory (first index varies the fastest). F order means that column-wise
operations will be faster. dtype : [optional, float(byDefault)] Data type of returned array.
In [56]: 1 import numpy as geek
2
3 b = [Link](2, dtype = int)
4 print("Matrix b : \n", b)
5
6 a = [Link]([2, 2], dtype = int)
7 print("\nMatrix a : \n", a)
8
9 c = [Link]([3, 3])
10 print("\nMatrix c : \n", c)

Matrix b :
[1 1]

Matrix a :
[[1 1]
[1 1]]

Matrix c :
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]

Numpy for Numerical Data Preprocessing

Silcing

A[Start:end:step]

In [57]: 1 A=[Link](100)
2 A

Out[57]: array([ 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, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])

In [58]: 1 b=A[3:10]

In [59]: 1 b

Out[59]: array([3, 4, 5, 6, 7, 8, 9])

In [60]: 1 b[0]=-1200
In [61]: 1 b

Out[61]: array([-1200, 4, 5, 6, 7, 8, 9])

In [62]: 1 A

Out[62]: array([ 0, 1, 2, -1200, 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, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 62,
63, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 80,
81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98,
99])

In [63]: 1 b=A[3:10].copy()

In [64]: 1 b

Out[64]: array([-1200, 4, 5, 6, 7, 8, 9])

In [65]: 1 A[::5]

Out[65]: array([ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,
85, 90, 95])

In [66]: 1 A[::-5]

Out[66]: array([99, 94, 89, 84, 79, 74, 69, 64, 59, 54, 49, 44, 39, 34, 29, 24, 19,
14, 9, 4])
In [67]: 1 A[::-1]

Out[67]: array([ 99, 98, 97, 96, 95, 94, 93, 92, 91,
90, 89, 88, 87, 86, 85, 84, 83, 82,
81, 80, 79, 78, 77, 76, 75, 74, 73,
72, 71, 70, 69, 68, 67, 66, 65, 64,
63, 62, 61, 60, 59, 58, 57, 56, 55,
54, 53, 52, 51, 50, 49, 48, 47, 46,
45, 44, 43, 42, 41, 40, 39, 38, 37,
36, 35, 34, 33, 32, 31, 30, 29, 28,
27, 26, 25, 24, 23, 22, 21, 20, 19,
18, 17, 16, 15, 14, 13, 12, 11, 10,
9, 8, 7, 6, 5, 4, -1200, 2, 1,
0])

In [68]: 1 [Link]?

In [69]: 1 idx=[Link](A==-1200)[0][0]# get an index of an element


2 # in an array

In [70]: 1 idx

Out[70]: 3

[Link](arr)

arr : [array_like] Input array.

function is used to find the indices of array elements that are non-zero, grouped by element

In [71]: 1 A[idx]=3

In [72]: 1 A

Out[72]: array([ 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, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
In [73]: 1 A=[Link](10*[Link](5,4))

In [74]: 1 A

Out[74]: array([[ 3., 6., 9., 3.],


[ 8., 5., 5., 6.],
[ 8., 7., 10., 2.],
[ 1., 4., 8., 4.],
[ 7., 2., 1., 4.]])

In [75]: 1 A[1,2]

Out[75]: 5.0

In [76]: 1 A[1,:]

Out[76]: array([8., 5., 5., 6.])

In [77]: 1 A[:,1]

Out[77]: array([6., 5., 7., 4., 2.])

In [78]: 1 A[::2,:]

Out[78]: array([[ 3., 6., 9., 3.],


[ 8., 7., 10., 2.],
[ 7., 2., 1., 4.]])

In [79]: 1 A[1:3,2:4]

Out[79]: array([[ 5., 6.],


[10., 2.]])

In [80]: 1 # transpose of a matrix


2 A.T

Out[80]: array([[ 3., 8., 8., 1., 7.],


[ 6., 5., 7., 4., 2.],
[ 9., 5., 10., 8., 1.],
[ 3., 6., 2., 4., 4.]])

In [81]: 1 # importing a linear algebra library from numpy


2 import [Link] as la
3 [Link]([Link](3,3))
4 # it has given inverse of a matrix

Out[81]: array([[-0.41094744, 0.0620272 , 1.82170153],


[-4.14641955, 2.88435449, 3.81370105],
[ 5.37006788, -1.80905805, -6.01952272]])
In [82]: 1 [Link](axis=0)# it will sort a array according to a column

In [83]: 1 A

Out[83]: array([[ 1., 2., 1., 2.],


[ 3., 4., 5., 3.],
[ 7., 5., 8., 4.],
[ 8., 6., 9., 4.],
[ 8., 7., 10., 6.]])

In [84]: 1 [Link](axis=1)# it sorted according to row

In [85]: 1 A

Out[85]: array([[ 1., 1., 2., 2.],


[ 3., 3., 4., 5.],
[ 4., 5., 7., 8.],
[ 4., 6., 8., 9.],
[ 6., 7., 8., 10.]])

A[index_array]
In [86]: 1 A=[Link](100)
2 B=A[[3,5,6]]

In [87]: 1 B

Out[87]: array([3, 5, 6])

In [88]: 1 B[0]=-4
2 B

Out[88]: array([-4, 5, 6])

In [89]: 1 B=A[A<40] # it has given elemnt less than 40 in an array

In [90]: 1 B

Out[90]: array([ 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, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39])
In [91]: 1 B=A[(A<40)& (A>30)]# Bitwise And operator

In [92]: 1 B

Out[92]: array([31, 32, 33, 34, 35, 36, 37, 38, 39])

&-array,and-object

/-array, or-object

~ array,not -object

[Link](tup)
[Link]() function is used to stack the sequence of input arrays horizontally (i.e. column
wise) to make a single array.

Syntax : [Link](tup)

Parameters : tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays
must have the same shape along all but the second axis.

Return : [stacked ndarray] The stacked array of the input arrays.

In [93]: 1 import numpy as geek


2
3 # input array
4 in_arr1 = [Link]([ 1, 2, 3] )
5 print ("1st Input array : \n", in_arr1)
6
7 in_arr2 = [Link]([ 4, 5, 6] )
8 print ("2nd Input array : \n", in_arr2)
9
10 # Stacking the two arrays horizontally
11 out_arr = [Link]((in_arr1, in_arr2))
12 print ("Output horizontally stacked array:\n ", out_arr)

1st Input array :


[1 2 3]
2nd Input array :
[4 5 6]
Output horizontally stacked array:
[1 2 3 4 5 6]
In [94]: 1 import numpy as geek
2
3 # input array
4 in_arr1 = [Link]([[ 1, 2, 3], [ -1, -2, -3]] )
5 print ("1st Input array : \n", in_arr1)
6
7 in_arr2 = [Link]([[ 4, 5, 6], [ -4, -5, -6]] )
8 print ("2nd Input array : \n", in_arr2)
9
10 # Stacking the two arrays horizontally
11 out_arr = [Link]((in_arr1, in_arr2))
12 print ("Output stacked array :\n ", out_arr)

1st Input array :


[[ 1 2 3]
[-1 -2 -3]]
2nd Input array :
[[ 4 5 6]
[-4 -5 -6]]
Output stacked array :
[[ 1 2 3 4 5 6]
[-1 -2 -3 -4 -5 -6]]

[Link](tup)

[Link]() function is used to stack the sequence of input arrays vertically to make a
single array.

Syntax : [Link](tup)

Parameters : tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays
must have the same shape along all but the first axis.

Return : [stacked ndarray] The stacked array of the input arrays.


In [95]: 1 # Python program explaining
2 # vstack() function
3
4 import numpy as geek
5
6 # input array
7 in_arr1 = [Link]([ 1, 2, 3] )
8 print ("1st Input array : \n", in_arr1)
9
10 in_arr2 = [Link]([ 4, 5, 6] )
11 print ("2nd Input array : \n", in_arr2)
12
13 # Stacking the two arrays vertically
14 out_arr = [Link]((in_arr1, in_arr2))
15 print ("Output vertically stacked array:\n ", out_arr)

1st Input array :


[1 2 3]
2nd Input array :
[4 5 6]
Output vertically stacked array:
[[1 2 3]
[4 5 6]]

In [96]: 1 import numpy as geek


2
3 # input array
4 in_arr1 = [Link]([[ 1, 2, 3], [ -1, -2, -3]] )
5 print ("1st Input array : \n", in_arr1)
6
7 in_arr2 = [Link]([[ 4, 5, 6], [ -4, -5, -6]] )
8 print ("2nd Input array : \n", in_arr2)
9
10 # Stacking the two arrays vertically
11 out_arr = [Link]((in_arr1, in_arr2))
12 print ("Output stacked array :\n ", out_arr)

1st Input array :


[[ 1 2 3]
[-1 -2 -3]]
2nd Input array :
[[ 4 5 6]
[-4 -5 -6]]
Output stacked array :
[[ 1 2 3]
[-1 -2 -3]
[ 4 5 6]
[-4 -5 -6]]

[Link]()
arr : Array to be sorted.
axis : Axis along which we need array to be started.

order : This argument specifies which fields to compare first.

kind : [‘quicksort’{default}, ‘mergesort’, ‘heapsort’]Sorting algorithm.

In [97]: 1 # importing libraries


2 import numpy as np
3
4 # sort along the first axis
5 a = [Link]([[12, 15], [10, 1]])
6 arr1 = [Link](a, axis = 0)
7 print ("Along first axis : \n", arr1)
8
9
10 # sort along the last axis
11 a = [Link]([[10, 15], [12, 1]])
12 arr2 = [Link](a, axis = -1)
13 print ("\nAlong first axis : \n", arr2)
14
15
16 a = [Link]([[12, 15], [10, 1]])
17 arr1 = [Link](a, axis = None)
18 print ("\nAlong none axis : \n", arr1)

Along first axis :


[[10 1]
[12 15]]

Along first axis :


[[10 15]
[ 1 12]]

Along none axis :


[ 1 10 12 15]

Pandas
In [98]: 1 import pandas as pd

class [Link](data=None, index=None, dtype=None, name=None, copy=None,


fastpath=False)

One-dimensional ndarray with axis labels (including time series)

Parameters:

data-array-like, Iterable, dict, or scalar value Contains data stored in Series. If data is a dict,
argument order is maintained.
index-array-like or Index (1d) Values must be hashable and have the same length as data.
Non-unique index values are allowed. Will default to RangeIndex (0, 1, 2, …, n) if not provided.
If data is dict-like and index is None, then the keys in the data are used as the index. If the
index is not None, the resulting Series is reindexed with the index values.

dtype-str, [Link], or ExtensionDtype, optional Data type for the output Series. If not
specified, this will be inferred from data. See the user guide for more usages.

name-Hashable, default None The name to give to the Series.

copy-bool default False Copy input data Only affects Series or 1d ndarray input See

In [99]: 1 d= {'a': 1, 'b': 2, 'c': 3}


2 ser = [Link](data=d, index=['a', 'b', 'c'])
3 ser

Out[99]: a 1
b 2
c 3
dtype: int64

The keys of the dictionary match with the Index values, hence the Index values have no effect

In [100]: 1 d = {'a': 1, 'b': 2, 'c': 3}


2 ser = [Link](data=d, index=['x', 'y', 'z'])
3 ser

Out[100]: x NaN
y NaN
z NaN
dtype: float64

Note that the Index is first build with the keys from the dictionary. After this the Series is
reindexed with the given Index values, hence we get all NaN as a result.

In [101]: 1 #Constructing Series from a list with copy=False.


2 r = [1, 2]
3 ser = [Link](r, copy=False)
4 [Link][0] = 999
5 r

Out[101]: [1, 2]

In [102]: 1 print(ser)

0 999
1 2
dtype: int64

Due to input data type the Series has a copy of the original data even though copy=False, so
the data is unchanged.
In [103]: 1 data=[Link]([0.25,0.3,5,7],index=["1","2","3","5"])
2 data

Out[103]: 1 0.25
2 0.30
3 5.00
5 7.00
dtype: float64

In [104]: 1 [Link]# values

Out[104]: array([0.25, 0.3 , 5. , 7. ])

In [105]: 1 [Link] # index of a value

Out[105]: Index(['1', '2', '3', '5'], dtype='object')

In [106]: 1 # checking a pandas version


2 pd.__version__

Out[106]: '1.3.4'

In [107]: 1 type([Link])

Out[107]: [Link]

In [108]: 1 type([Link])

Out[108]: [Link]

In [109]: 1 data["2":"5"]

Out[109]: 2 0.3
3 5.0
5 7.0
dtype: float64

In [110]: 1 grades_dict={"A":2,"B":2.5,"C":3,"D":3.5}
2 grads=[Link](grades_dict)
3 grads

Out[110]: A 2.0
B 2.5
C 3.0
D 3.5
dtype: float64
In [111]: 1 marks_dict={"A":85,"B":75,"C":70,"D":65}
2 marks=[Link](marks_dict)

In [112]: 1 marks

Out[112]: A 85
B 75
C 70
D 65
dtype: int64

[Link](data=None, index=None,
columns=None, dtype=None, copy=None)
[source]
Two-dimensional, size-mutable, potentially heterogeneous tabular data.

Data structure also contains labeled axes (rows and columns). Arithmetic operations align on
both row and column labels. Can be thought of as a dict-like container for Series objects. The
primary pandas data structure.

Parameters:

data:ndarray (structured or homogeneous), Iterable, dict, or DataFrame Dict can contain


Series, arrays, constants, dataclass or list-like objects. If data is a dict, column order follows
insertion-order. If a dict contains Series which have an index defined, it is aligned by its index.
This alignment also occurs if data is a Series or a DataFrame itself. Alignment is done on
Series/DataFrame inputs.

If data is a list of dicts, column order follows insertion-order.

index:Index or array-like Index to use for resulting frame. Will default to RangeIndex if no
indexing information part of input data and no index provided.

columns:Index or array-like Column labels to use for resulting frame when data does not have
them, defaulting to RangeIndex(0, 1, 2, …, n). If data contains column labels, will perform
column selection instead.

dtype:dtype, default None Data type to force. Only a single dtype is allowed. If None, infer.

copy:bool or None, default None Copy data from inputs. For dict data, the default of None
behaves like copy=True. For DataFrame or 2d ndarray input, the default of None behaves like
copy=False. If data is a dict containing one or more Series (possibly of different dtypes),
copy=False will ensure that these inputs are not copied.
In [113]: 1 d = {'col1': [1, 2], 'col2': [3, 4]}
2 df = [Link](data=d)
3 df

Out[113]:
col1 col2

0 1 3

1 2 4

In [114]: 1 [Link]

Out[114]: col1 int64


col2 int64
dtype: object

In [115]: 1 df = [Link](data=d, dtype=np.int8)


2 [Link]

Out[115]: col1 int8


col2 int8
dtype: object

In [116]: 1 d = {'col1': [0, 1, 2, 3], 'col2': [Link]([2, 3], index=[2, 3])}


2 [Link](data=d, index=[0, 1, 2, 3])

Out[116]:
col1 col2

0 0 NaN

1 1 NaN

2 2 2.0

3 3 3.0

In [117]: 1 rs=[Link]({"grades":grads,"marks":marks})
2 rs

Out[117]:
grades marks

A 2.0 85

B 2.5 75

C 3.0 70

D 3.5 65
In [118]: 1 rs.T #transpose

Out[118]:
A B C D

grades 2.0 2.5 3.0 3.5

marks 85.0 75.0 70.0 65.0

In [119]: 1 [Link]

Out[119]: array([[ 2. , 85. ],


[ 2.5, 75. ],
[ 3. , 70. ],
[ 3.5, 65. ]])

In [120]: 1 [Link][2,0]# two dimensional array consider

Out[120]: 3.0

In [121]: 1 [Link]# columns names

Out[121]: Index(['grades', 'marks'], dtype='object')

In [122]: 1 # creating a new column in a rs dataframe


2 rs["Scale_marks"]=rs["marks"]/90*100

In [123]: 1 rs

Out[123]:
grades marks Scale_marks

A 2.0 85 94.444444

B 2.5 75 83.333333

C 3.0 70 77.777778

D 3.5 65 72.222222

In [124]: 1 del rs["Scale_marks"]

In [125]: 1 rs

Out[125]:
grades marks

A 2.0 85

B 2.5 75

C 3.0 70

D 3.5 65
In [126]: 1 rs[rs["marks"]>75]

Out[126]:
grades marks

A 2.0 85

In [127]: 1 A=[Link]([{'a':1,"b":2},{"b":7,"c":2}])

In [128]: 1 A

Out[128]:
a b c

0 1.0 2 NaN

1 NaN 7 2.0

In [129]: 1 # for checking a null values in data frame


2 [Link]().sum()

Out[129]: a 1
b 0
c 1
dtype: int64

In [130]: 1 [Link]()

Out[130]:
a b c

0 False False True

1 True False False

In [131]: 1 A=[Link](0)

In [132]: 1 A

Out[132]:
a b c

0 1.0 2 0.0

1 0.0 7 2.0

In [133]: 1 [Link]?
In [134]: 1 A=[Link](["a","b","c"],index=[1,3,4])
2 A

Out[134]: 1 a
3 b
4 c
dtype: object

In [135]: 1 A[1]

Out[135]: 'a'

In [136]: 1 A[1:3]

Out[136]: 3 b
4 c
dtype: object

In [137]: 1 [Link][1:3]

Out[137]: 1 a
3 b
dtype: object

In [138]: 1 [Link][1:3]

Out[138]: 3 b
4 c
dtype: object

In [139]: 1 [Link][:,0:]

Out[139]:
grades marks

A 2.0 85

B 2.5 75

C 3.0 70

D 3.5 65

Covid-19 dataset
In [140]: 1 import pandas as pd
2 import numpy as np
3 from [Link] import SimpleImputer
In [141]: 1 df=pd.read_csv(r"C:\Users\VAISHNAVI\Downloads\[Link]")

In [142]: 1 df

Out[142]:
Deaths
New New New
Country/Region Confirmed Deaths Recovered Active / 100
cases deaths recovered
Cases

0 Afghanistan 36263 1269 25198 9796 106 10 18 3.50

1 Albania 4880 144 2745 1991 117 6 63 2.95

2 Algeria 27973 1163 18837 7973 616 8 749 4.16

3 Andorra 907 52 803 52 10 0 0 5.73

4 Angola 950 41 242 667 18 1 0 4.32

... ... ... ... ... ... ... ... ... ...

West Bank and


182 10621 78 3752 6791 152 2 0 0.73
Gaza

183 Western Sahara 10 1 8 1 0 0 0 10.00

184 Yemen 1691 483 833 375 10 4 36 28.56

185 Zambia 4552 140 2815 1597 71 1 465 3.08

186 Zimbabwe 2704 36 542 2126 192 2 24 1.33

187 rows × 15 columns

In [143]: 1 [Link]

Out[143]: Index(['Country/Region', 'Confirmed', 'Deaths', 'Recovered', 'Active',


'New cases', 'New deaths', 'New recovered', 'Deaths / 100 Cases',
'Recovered / 100 Cases', 'Deaths / 100 Recovered',
'Confirmed last week', '1 week change', '1 week % increase',
'WHO Region'],
dtype='object')
In [144]: 1 [Link]()

<class '[Link]'>
RangeIndex: 187 entries, 0 to 186
Data columns (total 15 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Country/Region 187 non-null object
1 Confirmed 187 non-null int64
2 Deaths 187 non-null int64
3 Recovered 187 non-null int64
4 Active 187 non-null int64
5 New cases 187 non-null int64
6 New deaths 187 non-null int64
7 New recovered 187 non-null int64
8 Deaths / 100 Cases 187 non-null float64
9 Recovered / 100 Cases 187 non-null float64
10 Deaths / 100 Recovered 187 non-null float64
11 Confirmed last week 187 non-null int64
12 1 week change 187 non-null int64
13 1 week % increase 187 non-null float64
14 WHO Region 187 non-null object
dtypes: float64(4), int64(9), object(2)
memory usage: 22.0+ KB

In [145]: 1 [Link]

Out[145]: Country/Region object


Confirmed int64
Deaths int64
Recovered int64
Active int64
New cases int64
New deaths int64
New recovered int64
Deaths / 100 Cases float64
Recovered / 100 Cases float64
Deaths / 100 Recovered float64
Confirmed last week int64
1 week change int64
1 week % increase float64
WHO Region object
dtype: object
In [146]: 1 [Link]().sum()

Out[146]: Country/Region 0
Confirmed 0
Deaths 0
Recovered 0
Active 0
New cases 0
New deaths 0
New recovered 0
Deaths / 100 Cases 0
Recovered / 100 Cases 0
Deaths / 100 Recovered 0
Confirmed last week 0
1 week change 0
1 week % increase 0
WHO Region 0
dtype: int64

In [147]: 1 [Link]()

Out[147]:
Deaths R
New New New
Country/Region Confirmed Deaths Recovered Active / 100
cases deaths recovered
Cases

0 Afghanistan 36263 1269 25198 9796 106 10 18 3.50

1 Albania 4880 144 2745 1991 117 6 63 2.95

2 Algeria 27973 1163 18837 7973 616 8 749 4.16

3 Andorra 907 52 803 52 10 0 0 5.73

4 Angola 950 41 242 667 18 1 0 4.32


In [148]: 1 [Link](10)

Out[148]:
Deaths R
New New New
Country/Region Confirmed Deaths Recovered Active / 100
cases deaths recovered
Cases

0 Afghanistan 36263 1269 25198 9796 106 10 18 3.50

1 Albania 4880 144 2745 1991 117 6 63 2.95

2 Algeria 27973 1163 18837 7973 616 8 749 4.16

3 Andorra 907 52 803 52 10 0 0 5.73

4 Angola 950 41 242 667 18 1 0 4.32

Antigua and
5 86 3 65 18 4 0 5 3.49
Barbuda

6 Argentina 167416 3059 72575 91782 4890 120 2057 1.83

7 Armenia 37390 711 26665 10014 73 6 187 1.90

8 Australia 15303 167 9311 5825 368 6 137 1.09

9 Austria 20558 713 18246 1599 86 1 37 3.47

In [149]: 1 [Link]()

Out[149]:
Deaths
New New New
Country/Region Confirmed Deaths Recovered Active / 100
cases deaths recovered
Cases

West Bank and


182 10621 78 3752 6791 152 2 0 0.73
Gaza

183 Western Sahara 10 1 8 1 0 0 0 10.00

184 Yemen 1691 483 833 375 10 4 36 28.56

185 Zambia 4552 140 2815 1597 71 1 465 3.08

186 Zimbabwe 2704 36 542 2126 192 2 24 1.33


In [150]: 1 [Link](10)

Out[150]:
Deaths
New New New
Country/Region Confirmed Deaths Recovered Active / 100
cases deaths recovered
Cases

177 United Kingdom 301708 45844 1437 254427 688 7 3 15.19

178 Uruguay 1202 35 951 216 10 1 3 2.91

179 Uzbekistan 21209 121 11674 9414 678 5 569 0.57

180 Venezuela 15988 146 9959 5883 525 4 213 0.91

181 Vietnam 431 0 365 66 11 0 0 0.00

West Bank and


182 10621 78 3752 6791 152 2 0 0.73
Gaza

183 Western Sahara 10 1 8 1 0 0 0 10.00

184 Yemen 1691 483 833 375 10 4 36 28.56

185 Zambia 4552 140 2815 1597 71 1 465 3.08

186 Zimbabwe 2704 36 542 2126 192 2 24 1.33

In [151]: 1 [Link]

Out[151]: (187, 15)

In [152]: 1 df2=[Link]([df["Country/Region"],df["WHO Region"]])[["Deaths","Recove


In [153]: 1 df2

Out[153]:
New New New
Country/Region WHO Region Deaths Recovered Active
cases deaths recovered

Eastern
0 Afghanistan 1269 25198 9796 106 10 18
Mediterranean

1 Albania Europe 144 2745 1991 117 6 63

2 Algeria Africa 1163 18837 7973 616 8 749

3 Andorra Europe 52 803 52 10 0 0

4 Angola Africa 41 242 667 18 1 0

... ... ... ... ... ... ... ... ...

West Bank and Eastern


182 78 3752 6791 152 2 0
Gaza Mediterranean

183 Western Sahara Africa 1 8 1 0 0 0

Eastern
184 Yemen 483 833 375 10 4 36
Mediterranean

185 Zambia Africa 140 2815 1597 71 1 465

186 Zimbabwe Africa 36 542 2126 192 2 24

187 rows × 8 columns

[Link](by=None, axis=0, level=None, as_index=True, sort=True,


group_keys=True, observed=False, dropna=True)

Group DataFrame using a mapper or by a Series of columns.

A groupby operation involves some combination of splitting the object, applying a function, and
combining the results. This can be used to group large amounts of data and compute
operations on these groups.

Parameters:

by:mapping, function, label, [Link] or list of such Used to determine the groups for the
groupby. If by is a function, it’s called on each value of the object’s index. If a dict or Series is
passed, the Series or dict VALUES will be used to determine the groups (the Series’ values are
first aligned; see .align() method). If a list or ndarray of length equal to the selected axis is
passed (see the groupby user guide), the values are used as-is to determine the groups. A
label or list of labels may be passed to group by the columns in self. Notice that a tuple is
interpreted as a (single) key.

axis:{0 or ‘index’, 1 or ‘columns’}, default 0 Split along rows (0) or columns (1). For Series this
parameter is unused and defaults to 0.

level:int, level name, or sequence of such, default None If the axis is a MultiIndex
(hierarchical), group by a particular level or levels. Do not specify both by and level.
as_index:bool, default True For aggregated output, return object with group labels as the index.
Only relevant for DataFrame input. as_index=False is effectively “SQL-style” grouped output.

sort:bool, default True Sort group keys. Get better performance by turning this off. Note this
does not influence the order of observations within each group. Groupby preserves the order of
rows within each group.

Changed in version 2.0.0: Specifying sort=False with an ordered categorical grouper will no
longer sort the values.

group_keys:bool, default True When calling apply and the by argument produces a like-
indexed (i.e. a transform) result, add group keys to index to identify pieces. By default group
keys are not included when the result’s index (and column) labels match the inputs, and are
included otherwise.

Changed in version 1.5.0: Warns that group_keys will no longer be ignored when the result
from apply is a like-indexed Series or DataFrame. Specify group_keys explicitly to include the
group keys or not.

Changed in version 2.0.0: group_keys now defaults to True.

observed:bool, default False This only applies if any of the groupers are Categoricals. If True:
only show observed values for categorical groupers. If False: show all values for categorical
groupers.

dropna:bool, default True If True, and if group keys contain NA values, NA values together with
row/column will be dropped. If False, NA values will also be treated as the key in groups.

In [154]: 1 [Link](columns={"Country/Region":"Country","WHO Region":"Region"},inpl


In [155]: 1 df

Out[155]:
Deaths Rec
New New New
Country Confirmed Deaths Recovered Active / 100
cases deaths recovered
Cases

0 Afghanistan 36263 1269 25198 9796 106 10 18 3.50

1 Albania 4880 144 2745 1991 117 6 63 2.95

2 Algeria 27973 1163 18837 7973 616 8 749 4.16

3 Andorra 907 52 803 52 10 0 0 5.73

4 Angola 950 41 242 667 18 1 0 4.32

... ... ... ... ... ... ... ... ... ...

West Bank
182 10621 78 3752 6791 152 2 0 0.73
and Gaza

Western
183 10 1 8 1 0 0 0 10.00
Sahara

184 Yemen 1691 483 833 375 10 4 36 28.56

185 Zambia 4552 140 2815 1597 71 1 465 3.08

186 Zimbabwe 2704 36 542 2126 192 2 24 1.33

187 rows × 15 columns

[Link](percentiles=None, include=None, exclude=None)

Generate descriptive statistics.

Descriptive statistics include those that summarize the central tendency, dispersion and shape
of a dataset’s distribution, excluding NaN values.

Analyzes both numeric and object series, as well as DataFrame column sets of mixed data
types. The output will vary depending on what is provided. Refer to the notes below for more
detail.

percentile:slist-like of numbers, optional The percentiles to include in the output. All should fall
between 0 and 1. The default is [.25, .5, .75], which returns the 25th, 50th, and 75th
percentiles.

include:‘all’, list-like of dtypes or None (default), optional A white list of data types to include in
the result. Ignored for Series. Here are the options:

‘all’ : All columns of the input will be included in the output.


A list-like of dtypes : Limits the results to the provided data types. To limit the result to numeric
types submit [Link]. To limit it instead to object columns submit the [Link] data
type. Strings can also be used in the style of select_dtypes (e.g. [Link](include=['O'])). To
select pandas categorical columns, use 'category'

None (default) : The result will include all numeric columns.

exclude:list-like of dtypes or None (default), optional, A black list of data types to omit from the
result. Ignored for Series. Here are the options:

A list-like of dtypes : Excludes the provided data types from the re


sult. To exclude numeric types submit [Link]. To exclude object
columns submit the data type [Link]. Strings can also be used i
n the style of select_dtypes (e.g. [Link](exclude=['O'])). To ex
clude pandas categorical columns use 'category'

In [156]: 1 [Link]()

Out[156]:

Confirmed Deaths Recovered Active New cases New deaths

count 1.870000e+02 187.000000 1.870000e+02 1.870000e+02 187.000000 187.000000

mean 8.813094e+04 3497.518717 5.063148e+04 3.400194e+04 1222.957219 28.957219

std 3.833187e+05 14100.002482 1.901882e+05 2.133262e+05 5710.374790 120.037173 4

min 1.000000e+01 0.000000 0.000000e+00 0.000000e+00 0.000000 0.000000

25% 1.114000e+03 18.500000 6.265000e+02 1.415000e+02 4.000000 0.000000

50% 5.059000e+03 108.000000 2.815000e+03 1.600000e+03 49.000000 1.000000

75% 4.046050e+04 734.000000 2.260600e+04 9.149000e+03 419.500000 6.000000

max 4.290259e+06 148011.000000 1.846641e+06 2.816444e+06 56336.000000 1076.000000 33

In [157]: 1 df1=[Link](df["Country"])[["Confirmed","Deaths"]].sum()
In [158]: 1 df1

Out[158]:
Confirmed Deaths

Country

Afghanistan 36263 1269

Albania 4880 144

Algeria 27973 1163

Andorra 907 52

Angola 950 41

... ... ...

West Bank and Gaza 10621 78

Western Sahara 10 1

Yemen 1691 483

Zambia 4552 140

Zimbabwe 2704 36

187 rows × 2 columns

In [159]: 1 df3=df1[df1["Confirmed"]>1000]
2 df3

Out[159]:
Confirmed Deaths

Country

Afghanistan 36263 1269

Albania 4880 144

Algeria 27973 1163

Argentina 167416 3059

Armenia 37390 711

... ... ...

Venezuela 15988 146

West Bank and Gaza 10621 78

Yemen 1691 483

Zambia 4552 140

Zimbabwe 2704 36

142 rows × 2 columns


In [160]: 1 import [Link] as plt

In [161]: 1 x=[Link](0,10,1000)
2 [Link](x,[Link](x))

Out[161]: [<[Link].Line2D at 0x228d80ef670>]

The [Link]() function returns number spaces evenly w.r.t interval.

[Link](start, stop, num = 50, endpoint = True, retstep = False, dtype = None)

-> start : [optional] start of interval range. By default start = 0

-> stop : end of interval range

-> restep : If True, return (samples, step). By default restep = False

-> num : [int, optional] No. of samples to generate

-> dtype : type of output array

In [162]: 1 import numpy as geek


2
3 # restep set to True
4 print("B\n", [Link](2.0, 3.0, num=5, retstep=True), "\n")
5
6 # To evaluate sin() in long range
7 x = [Link](0, 2, 10)
8 print("A\n", [Link](x))

B
(array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)

A
[0. 0.22039774 0.42995636 0.6183698 0.77637192 0.8961922
0.9719379 0.99988386 0.9786557 0.90929743]
plot() function in pyplot module of matplotlib library is used to make a 2D hexagonal binning
plot of points x, y.

[Link](*args, scalex=True, scaley=True, data=None, **kwargs)

x, y: These parameter are the horizontal and vertical coordinates of the data points. x values
are optional.

fmt: This parameter is an optional parameter and it contains the string value.

data: This parameter is an optional parameter and it is an object with labelled data.

In [163]: 1 import [Link] as plt


2 import numpy as np
3
4 [Link]([1, 2, 3])
5 [Link]('[Link]() example 1')
6 [Link]()
7 [Link]()
In [164]: 1 [Link](figsize=(12,8))
2 [Link](df["New cases"],df["New deaths"],marker="^",s=30)
3 [Link]("Scatter plot between New cases vs new deaths")
4 [Link]("New cases")
5 [Link]("New deaths")

Out[164]: Text(0, 0.5, 'New deaths')

[Link]()
Scatter plots are used to observe relationship between variables and uses dots to represent
the relationship between them. The scatter() method in the matplotlib library is used to draw a
scatter plot. Scatter plots are widely used to represent relation among variables and how
change in one affects the other.

[Link](x_axis_data, y_axis_data, s=None, c=None, marker=None,


cmap=None, vmin=None, vmax=None, alpha=None, linewidths=None,
edgecolors=None)

x_axis_data- An array containing x-axis data

y_axis_data- An array containing y-axis data

s- marker size (can be scalar or array of size equal to size of x or y)

c- color of sequence of colors for markers


marker- marker style

cmap- cmap name

linewidths- width of marker border

edgecolor- marker border color

alpha- blending value between 0 (transparent) and 1 (opaque)

In [165]: 1 import [Link] as plt


2
3
4 x =[5, 7, 8, 7, 2, 17, 2, 9,
5 4, 11, 12, 9, 6]
6
7 y =[99, 86, 87, 88, 100, 86,
8 103, 87, 94, 78, 77, 85, 86]
9
10 [Link](x, y, c ="blue")
11
12 # To show the plot
13 [Link]()
In [166]: 1 import [Link] as plt
2
3 # dataset-1
4 x1 = [89, 43, 36, 36, 95, 10,
5 66, 34, 38, 20]
6
7 y1 = [21, 46, 3, 35, 67, 95,
8 53, 72, 58, 10]
9
10 # dataset2
11 x2 = [26, 29, 48, 64, 6, 5,
12 36, 66, 72, 40]
13
14 y2 = [26, 34, 90, 33, 38,
15 20, 56, 2, 47, 15]
16
17 [Link](x1, y1, c ="pink",
18 linewidths = 2,
19 marker ="s",
20 edgecolor ="green",
21 s = 50)
22
23 [Link](x2, y2, c ="yellow",
24 linewidths = 2,
25 marker ="^",
26 edgecolor ="red",
27 s = 200)
28
29 [Link]("X-axis")
30 [Link]("Y-axis")
31 [Link]()

In [167]: 1 #[Link](x,x+0,"-g")# solid green


2 #[Link](x,x+0,"--c") # dashed cyan
3 #[Link](x,X+2,"-k")dashdot black
4 #[Link](x,x+3,":r") dotted red
In [168]: 1 import [Link] as plt
2 import seaborn as sns

In [169]: 1 [Link]("classic") # seaborn


2 x=[Link](0,100,1000)
3 y=[Link]([Link](1000,5),0)
4 [Link](x,y)
5 [Link]("ABCDE", ncol=2,loc="upper left")

Out[169]: <[Link] at 0x228d8136f40>


In [170]: 1 [Link]() # that is set for syle seaborn
2 [Link](x,y)
3 [Link]("ABCDE",ncol=2,loc="upper left")

Out[170]: <[Link] at 0x228d8756970>

In [196]: 1 [Link]()
2 x=[Link](50*[Link](50000))
3 y=[Link](200*[Link](60000))
4 z=[Link](100*[Link](50000)+1000)
5 data=[Link]({"X":x,"Y":y,"Z":z})
6 [Link]()

Out[196]:
X Y Z

0 -13.209391 -146.034778 961.619996

1 -35.807704 194.564975 975.105110

2 12.457982 411.415138 918.112732

3 -79.013461 126.440942 899.794462

4 -18.842445 -222.243822 940.930238


In [197]: 1 for col in [Link]:
2 [Link](data[col],density=True,alpha=0.6)
3 [Link](data[col],shade=True)

[Link]() Function

hist() function in pyplot module of matplotlib library is used to plot a histogram.

[Link](x, bins=None, range=None, density=False,


weights=None, cumulative=False, bottom=None, histtype=’bar’,
align=’mid’, orientation=’vertical’, rwidth=None, log=False,
color=None, label=None, stacked=False, *, data=None, **kwargs)

x : This parameter are the sequence of data.

bins : This parameter is an optional parameter and it contains the integer or sequence or string.

range : This parameter is an optional parameter and it the lower and upper range of the bins.

density : This parameter is an optional parameter and it contains the boolean values.

weights : This parameter is an optional parameter and it is an array of weights, of the same
shape as x.
bottom : This parameter is the location of the bottom baseline of each bin.

histtype : This parameter is an optional parameter and it is used to draw type of histogram.
{‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’}

align : This parameter is an optional parameter and it controls how the histogram is plotted.
{‘left’, ‘mid’, ‘right’}

rwidth : This parameter is an optional parameter and it is a relative width of the bars as a
fraction of the bin width

log : This parameter is an optional parameter and it is used to set histogram axis to a log scale

color : This parameter is an optional parameter and it is a color spec or sequence of color
specs, one per dataset.

label : This parameter is an optional parameter and it is a string, or sequence of strings to


match multiple datasets.

normed : This parameter is an optional parameter and it contains the boolean [Link] uses
the density keyword argument instead.
In [198]: 1 import matplotlib
2 import numpy as np
3 import [Link] as plt
4
5 [Link](10**7)
6 mu = 121
7 sigma = 21
8 x = mu + sigma * [Link](1000)
9
10 num_bins = 100
11
12 n, bins, patches = [Link](x, num_bins,
13 density = 1,
14 color ='green',
15 alpha = 0.7)
16
17 y = ((1 / ([Link](2 * [Link]) * sigma)) *
18 [Link](-0.5 * (1 / sigma * (bins - mu))**2))
19
20 [Link](bins, y, '--', color ='black')
21
22 [Link]('X-Axis')
23 [Link]('Y-Axis')
24
25 [Link]('[Link]() function Example\n\n',
26 fontweight ="bold")
27
28 [Link]()
n :This returns the values of the histogram bins.

bins :This returns the edges of the bins.

patches :This returns the list of individual patches used to create the histogram.
In [199]: 1 import matplotlib
2 import numpy as np
3 import [Link] as plt
4
5 [Link](10**7)
6 n_bins = 20
7 x = [Link](10000, 3)
8
9 colors = ['green', 'blue', 'lime']
10
11 [Link](x, n_bins, density = True,
12 histtype ='bar',
13 color = colors,
14 label = colors)
15
16 [Link](prop ={'size': 10})
17
18 [Link]('[Link]() function Example\n\n',
19 fontweight ="bold")
20
21 [Link]()
In [200]: 1 x=[Link](50*[Link](50000))
2 y=[Link](200*[Link](60000))
3 z=[Link](100*[Link](50000)+1000)

In [201]: 1 [Link](x,density=True,alpha=0.5)
2 [Link](y,density=True,alpha=0.5)
3 [Link](z,density=True,alpha=0.5)

Out[201]: (array([8.88334643e-07, 1.90991948e-05, 1.93879036e-04, 1.12596416e-03,


3.04565532e-03, 3.78630433e-03, 2.23127454e-03, 6.19613413e-04,
7.63967793e-05, 5.10792420e-06]),
array([ 522.80975709, 612.86590954, 702.92206198, 792.97821443,
883.03436687, 973.09051932, 1063.14667176, 1153.20282421,
1243.25897665, 1333.3151291 , 1423.37128154]),
<BarContainer object of 10 artists>)
In [202]: 1 for col in [Link]:
2 [Link](data[col])
[Link](a=None, bins=None, hist=True, kde=True,
rug=False, fit=None, hist_kws=None, kde_kws=None,
rug_kws=None, fit_kws=None, color=None, vertical=False,
norm_hist=False, axlabel=None, label=None, ax=None, x=None)

A Distplot or distribution plot, depicts the variation in the data distribution. Seaborn Distplot
represents the overall distribution of continuous data variables.

The Seaborn module along with the Matplotlib module is used to depict the distplot with
different variations in it. The Distplot depicts the data by a histogram and a line in combination
to it.

[Link]() function is used to plot the distplot.

The distplot represents the univariate distribution of data i.e. data distribution of a variable
against the density distribution.
In [203]: 1 data = [Link](200)
2 res = [Link](data)
3 [Link]()

C:\Users\VAISHNAVI\anaconda3\lib\site-packages\seaborn\[Link]
9: FutureWarning: `distplot` is a deprecated function and will be removed in
a future version. Please adapt your code to use either `displot` (a figure-l
evel function with similar flexibility) or `histplot` (an axes-level functio
n for histograms).
[Link](msg, FutureWarning)

We have used the [Link]() function to generate random data values. Further,
the [Link]() function is used show the plot.

In [205]: 1 data=[Link](data)
In [207]: 1 [Link](df)

Out[207]: <[Link] at 0x228db50f9d0>


In [208]: 1 [Link]()
2 x=[Link](50*[Link](50000))
3 y=[Link](200*[Link](60000))
4 z=[Link](100*[Link](50000)+1000)
5 data=[Link]({"X":x,"Y":y,"Z":z})
6 [Link]()

Out[208]:
X Y Z

0 48.570762 45.426980 877.119031

1 -32.486315 -92.710907 892.103618

2 65.729454 -51.403156 1109.081758

3 20.908687 84.550352 1061.779724

4 -26.906131 -204.637740 986.757410


In [209]: 1 [Link](data)

Out[209]: <[Link] at 0x228e7cf3700>

[Link]() :

To plot multiple pairwise bivariate distributions in a dataset, you can use the .pairplot() function.

The diagonal plots are the univariate plots, and this displays the relationship for the (n, 2)
combination of variables in a DataFrame as a matrix of plots.

[Link]( data, **kwargs )

EXAMPLE
In [253]: 1 from sklearn import datasets
2 import pandas as pd
3 ​
4 # load_boston() returns [Link]
5 iris= datasets.load_iris()
6 # boston_data.data is a 2D array
7 # boston_data.feature_names is an array of columns labels
8 df = [Link]([Link])
9 df['target'] = [Link]([Link])
10 [Link]()

Out[253]:
0 1 2 3 target

0 5.1 3.5 1.4 0.2 0

1 4.9 3.0 1.4 0.2 0

2 4.7 3.2 1.3 0.2 0

3 4.6 3.1 1.5 0.2 0

4 5.0 3.6 1.4 0.2 0


In [258]: 1 [Link](df,hue="target")

Out[258]: <[Link] at 0x228eaebee80>

In [259]: 1 from [Link] import figure,output_file,show

In [262]: 1 output_file("[Link]")
2 p=figure(plot_width=400,plot_height=400,title="line")
3 [Link]([1,2,3,4],[6,7,8,9],line_width=2)
4 show(p)

You can bokeh also for interactive plots

In [ ]: 1 ​

You might also like