0% found this document useful (0 votes)
3 views

asics-of-python-ipynb-colaboratory

The document is a Jupyter notebook that covers the basics of Python programming, focusing on data manipulation using NumPy and Pandas. It includes practical examples of creating arrays, matrices, and performing operations such as slicing, reshaping, and statistical calculations. Additionally, it demonstrates how to read and analyze data from CSV files using Pandas.

Uploaded by

jerih61447
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

asics-of-python-ipynb-colaboratory

The document is a Jupyter notebook that covers the basics of Python programming, focusing on data manipulation using NumPy and Pandas. It includes practical examples of creating arrays, matrices, and performing operations such as slicing, reshaping, and statistical calculations. Additionally, it demonstrates how to read and analyze data from CSV files using Pandas.

Uploaded by

jerih61447
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

12/30/23, 6:05 PM 1. Basics Of Python.

ipynb - Colaboratory

 Practical - 01

Aim - To Understand the basics of python

v1=[1,7,3]
v2=[5,6,7]
v3=v1+v2
print(v3)
print(type(v3))

[1, 7, 3, 5, 6, 7]
<class 'list'>

import numpy as manav


a=manav.array([1,2,3,4])
print(a)
print(a.shape)
b=manav.array([2,2,2,2])
print(b)
print(a+b)

[1 2 3 4]
(4,)
[2 2 2 2]
[3 4 5 6]

Vertical Vector using Numpy

import numpy as np
a=np.array([[1],[2],[3]])
print(type(a))
print(a)
print(a.shape)

<class 'numpy.ndarray'>
[[1]
[2]
[3]]
(3, 1)

Matrix using Numpy Array


12/30/23, 6:05 PM 1. Basics Of Python.ipynb - Colaboratory

import numpy as npa


a=npa.array([[1,2,3],[4,5,6,],[7,8,9]])
print(type(a))
print(a)
print(a.shape)

<class 'numpy.ndarray'>
[[1 2 3]
[4 5 6]
[7 8 9]]
(3, 3)

Converting Numpy array to numpy matrix

import numpy as np
a=np.array([1,2,3,4])
b=np.mat(a)
print(type(b))
print(b)
print(b.shape)

<class 'numpy.matrix'>
[[1 2 3 4]]
(1, 4)

Using Numpy Matrix

import numpy as np
a=np.matrix([[1,2,3],[4,5,6],[7,8,9]])
print(type(a))
print(a)
print(a.shape)

<class 'numpy.matrix'>
[[1 2 3]
[4 5 6]
[7 8 9]]
(3, 3)

Slicing the Vectors

import numpy as np
a=np.array([1,2,3,4,5,6,7,8,9])
print(a)
b=a[0:7:3]
print(b)
b=a[1:3:1]
print(b)

[1 2 3 4 5 6 7 8 9]
[1 4 7]
12/30/23, 6:05 PM 1. Basics Of Python.ipynb - Colaboratory
[2 3]

Slicing Matrix

import numpy as np
a=np.array([[1,2,3],[4,5,6],[7,8,9]])
print(a)
print('****************')
b=a[:]
print(b)
print('****************')
b=a[:1]
print(b)
print('****************')
b=a[1:]
print(b)
print('****************')
b=a[:,:1]
print(b)
print('****************')
b=a[:,1:]
print(b)
print('****************')
b=a[:,-1:]
print(b)

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

Arrange & Reshape


12/30/23, 6:05 PM 1. Basics Of Python.ipynb - Colaboratory

import numpy as np
a=np.arange(6).reshape((3,2))
print(type(a))
print(a)

<class 'numpy.ndarray'>
[[0 1]
[2 3]
[4 5]]

Special Matrices

import numpy as np
a=np.zeros(3)
print(a)
a=np.ones(3)
print(a)
a=np.random.rand(3)
print(a)
a=np.eye(3)
print(a)

[0. 0. 0.]
[1. 1. 1.]
[0.26919879 0.93760881 0.06652322]
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

Operations on Matrix

import numpy as np
a=np.matrix([[1,2,3],[4,5,6],[7,8,9]])
b=np.matrix([[3,2,1],[6,5,4],[9,8,7]])
print(a+b)
print(a-b)
print(a*b)
print(a/b)

[[ 4 4 4]
[10 10 10]
[16 16 16]]
[[-2 0 2]
[-2 0 2]
[-2 0 2]]
[[ 42 36 30]
[ 96 81 66]
[150 126 102]]
[[0.33333333 1. 3. ]
[0.66666667 1. 1.5 ]
[0.77777778 1. 1.28571429]]
12/30/23, 6:05 PM 1. Basics Of Python.ipynb - Colaboratory

Operations on matrix via functions

import numpy as np
a=np.matrix([[1,2,3],[4,5,6],[7,8,9]])
print(a.max())
print(a.min())
print(a.prod())
print(a.mean())
print(a.std())
print(a.var())
print(a.transpose())

9
1
362880
5.0
2.581988897471611
6.666666666666667
[[1 4 7]
[2 5 8]
[3 6 9]]

Introduction To Pandas

import numpy as np
import pandas as pd
a=pd.Series([1,2,3,4])
print(a)
b=pd.Series([1,2.5,3,4])
print(b)
c=pd.date_range("20230801", periods=6)
print(c)

0 1
1 2
2 3
3 4
dtype: int64
0 1.0
1 2.5
2 3.0
3 4.0
dtype: float64
DatetimeIndex(['2023-08-01', '2023-08-02', '2023-08-03', '2023-08-04',
'2023-08-05', '2023-08-06'],
dtype='datetime64[ns]', freq='D')

NUmpy array to Panda series


12/30/23, 6:05 PM 1. Basics Of Python.ipynb - Colaboratory

import numpy as np
import pandas as pd
a=np.array([1,2,3,4])
print(a)
print(type(a))
b=pd.Series(a)
print(b)
print(type(b))

[1 2 3 4]
<class 'numpy.ndarray'>
0 1
1 2
2 3
3 4
dtype: int64
<class 'pandas.core.series.Series'>

Pandas Dataframe

import numpy as np
import pandas as pd

v1=np.array([[1,2,3],[4,5,6]])
a=pd.DataFrame(v1, columns=list("ABC"))
print(a)

A B C
0 1 2 3
1 4 5 6

retrieving Data from csv


le for series generation using pandas

import pandas as pd

df=pd.read_csv("first.csv")
print("Displaying Full CSV File data:-")
print(df)
print("\nFirst Five Entries")
print(df.head())
print("\nLast five entries")
print(df.tail(5))
print("\nShow Total INdex")
print(df.index)
print("\nShowing datatypes fo columns")
print(df.columns)
print("\nShows dataframe, Method")
print(df.describe)
print("\nSorting the data in descending order")
print(df.sort_values(by="per",ascending=False))
print("\nTransposing the CSV File")
print(df.T)
12/30/23, 6:05 PM 1. Basics Of Python.ipynb - Colaboratory

Displaying Full CSV File data:-


srno Name per
0 1 Abb 80
1 2 acc 50
2 3 dfg 56
3 4 ser 45
4 5 ert 21
5 6 fhl 82
6 7 fjt 86
7 8 kgf 76
8 9 nnn 84
9 10 rft 53
10 11 tyg 54
11 12 yuh 82
12 13 ijk 31
13 14 okl 94
14 15 plk 68

First Five Entries


srno Name per
0 1 Abb 80
1 2 acc 50
2 3 dfg 56
3 4 ser 45
4 5 ert 21

Last five entries


srno Name per
10 11 tyg 54
11 12 yuh 82
12 13 ijk 31
13 14 okl 94
14 15 plk 68

Show Total INdex


RangeIndex(start=0, stop=15, step=1)

Showing datatypes fo columns


Index(['srno ', 'Name', 'per'], dtype='object')

Shows dataframe, Method


<bound method NDFrame.describe of srno Name per
0 1 Abb 80
1 2 acc 50
2 3 dfg 56
3 4 ser 45
4 5 ert 21
5 6 fhl 82
6 7 fjt 86
7 8 kgf 76
8 9 nnn 84
9 10 rft 53
10 11 tyg 54
11 12 yuh 82
12 13 ijk 31
13 14 okl 94
14 15 plk 68>

You might also like