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

In [1]: import numpy as np

import pandas as pd

In [26]: # Part 1: Perform the following operation on user created data using NumPy Lib

In [28]: # 1. Create the following Matrix and Perform the following operations on mat1 and mat2 using NumPy
mat1 = np.array([[10,20,30],[5,6,9],[11,15,17]])
mat2 = np.array([[1,20,3],[5,6,9],[2,1,3]])
print("1st matrix :\n",mat1)
print("\n2nd matrix :\n",mat2)

1st matrix :
[[10 20 30]
[ 5 6 9]
[11 15 17]]

2nd matrix :
[[ 1 20 3]
[ 5 6 9]
[ 2 1 3]]

In [12]: # Addition of two matrix


print("Addition of two matrix : \n",np.add(mat1,mat2))

Addition of two matrix :


[[11 40 33]
[10 12 18]
[13 16 20]]

In [13]: # Subtraction of two matrix


print("Subtraction of two matrix : \n",np.subtract(mat1,mat2))

Subtraction of two matrix :


[[ 9 0 27]
[ 0 0 0]
[ 9 14 14]]

In [14]: # Multiplication of two matrix


print("Multiplication of two matrix :\n",np.multiply(mat1,mat2))

Multiplication of two matrix :


[[ 10 400 90]
[ 25 36 81]
[ 22 15 51]]

In [15]: # Dot product


print("Dot product :\n",np.dot(mat1,mat2))

Dot product :
[[170 350 300]
[ 53 145 96]
[120 327 219]]

In [18]: # Find the diagonal element


print("diagonal element of mat1 :\n",np.diagonal(mat1))
print("\ndiagonal element of mat2 :\n",np.diagonal(mat2))

diagonal element of mat1 :


[10 6 17]

diagonal element of mat2 :


[1 6 3]

In [19]: # Find the Min and Max element from the Mat1
print("max of mat1 :",np.max(mat1))
print("min of mat2 :",np.min(mat1))

max of mat1 : 30
min of mat2 : 5

In [22]: # Find the product and sum of the element of the mat1 and mat2
print("product of the element of the mat1 :",np.product(mat1))
print("sum of the element of the mat1 :",np.sum(mat1))
print("product of the element of the mat2 :",np.product(mat2))
print("sum of the element of the mat2 :",np.sum(mat2))

product of the element of the mat1 : 4544100000


sum of the element of the mat1 : 123
product of the element of the mat2 : 97200
sum of the element of the mat2 : 50

In [24]: # Find the mean, median


print("mean of mat1 :",np.mean(mat1))
print("median of mat1 :",np.median(mat1))

mean of mat1 : 13.666666666666666


median of mat1 : 11.0

In [29]: # Part 2: Perform the following operation on user created data using Pandas Lib

In [85]: list1={"Hero":[1,5,6.6,8,6,13,6.6],"TVS":[11,13.4,18,9,16,13,18],"Honda":[26.6,50,80,None,1,0,80]}
index = ["Madhavi","Pranav","Bhagya","Param","Gauri","Anand","Bhagya"]
data1 = pd.DataFrame(list1,index)
data1

Out[85]: Hero TVS Honda

Madhavi 1.0 11.0 26.6

Pranav 5.0 13.4 50.0

Bhagya 6.6 18.0 80.0

Param 8.0 9.0 NaN

Gauri 6.0 16.0 1.0

Anand 13.0 13.0 0.0

Bhagya 6.6 18.0 80.0

In [33]: # Print first 5 rows of the data frame


data1.head(5)

Out[33]: Hero TVS Honda

Madhavi 1.0 11.0 26.6

Pranav 5.0 13.4 50.0

Bhagya 6.6 18.0 80.0

Param 8.0 9.0 NaN

Gauri 6.0 16.0 1.0

In [37]: # Print last 5 rows of the data frame


data1.tail(5)

Out[37]: Hero TVS Honda

Bhagya 6.6 18.0 80.0

Param 8.0 9.0 NaN

Gauri 6.0 16.0 1.0

Anand 13.0 13.0 0.0

Bhagya 6.6 18.0 80.0

In [39]: # Check the shape of a data frame


data1.shape

(7, 3)
Out[39]:

In [41]: # Display the complete information of the data frame


data1.describe()

Out[41]: Hero TVS Honda

count 7.000000 7.000000 6.000000

mean 6.600000 14.057143 39.600000

std 3.587014 3.448119 36.330703

min 1.000000 9.000000 0.000000

25% 5.500000 12.000000 7.400000

50% 6.600000 13.400000 38.300000

75% 7.300000 17.000000 72.500000

max 13.000000 18.000000 80.000000

In [43]: # Perform Transform operation on a data frame


data1.T

Out[43]: Madhavi Pranav Bhagya Param Gauri Anand Bhagya

Hero 1.0 5.0 6.6 8.0 6.0 13.0 6.6

TVS 11.0 13.4 18.0 9.0 16.0 13.0 18.0

Honda 26.6 50.0 80.0 NaN 1.0 0.0 80.0

In [44]: # Find the columns of a data frame


data1.columns

Index(['Hero', 'TVS', 'Honda'], dtype='object')


Out[44]:

In [45]: # Find the index of a data frame


data1.index

Index(['Madhavi', 'Pranav', 'Bhagya', 'Param', 'Gauri', 'Anand', 'Bhagya'], dtype='object')


Out[45]:

In [47]: # Calculate the mean median for certain Columns


print("mean of hero",np.mean(data1.Hero))
print("median of hero",np.median(data1.Hero))

mean of hero 6.6000000000000005


median of hero 6.6

In [53]: # Extract the data of Hero column into variable X (Using Index, Label and Location) and convert it into
# NumPy array
By_index = np.array(data1.iloc[0:,:1])
By_label = np.array(data1.Hero)
print(By_index)

[[ 1. ]
[ 5. ]
[ 6.6]
[ 8. ]
[ 6. ]
[13. ]
[ 6.6]]

In [91]: # 3. Perform operations like row/ column removal from the above-created data frame.
data1.drop('Honda',axis=1)

Out[91]: Hero TVS

Madhavi 1.0 11.0

Pranav 5.0 13.4

Bhagya 6.6 18.0

Gauri 6.0 16.0

Anand 13.0 13.0

Bhagya 6.6 18.0

In [86]: # 4. Handle the presence of duplicate data. (5)


data1.drop_duplicates()

Out[86]: Hero TVS Honda

Madhavi 1.0 11.0 26.6

Pranav 5.0 13.4 50.0

Bhagya 6.6 18.0 80.0

Param 8.0 9.0 NaN

Gauri 6.0 16.0 1.0

Anand 13.0 13.0 0.0

In [87]: #5. Check the presence of missing value


data1.isnull().sum()

Hero 0
Out[87]:
TVS 0
Honda 1
dtype: int64

In [88]: data1

Out[88]: Hero TVS Honda

Madhavi 1.0 11.0 26.6

Pranav 5.0 13.4 50.0

Bhagya 6.6 18.0 80.0

Param 8.0 9.0 NaN

Gauri 6.0 16.0 1.0

Anand 13.0 13.0 0.0

Bhagya 6.6 18.0 80.0

In [90]: # 6. Handle the missing value using dropna()


data1.copy()
data1.dropna(axis=0,inplace=True)
data1

Out[90]: Hero TVS Honda

Madhavi 1.0 11.0 26.6

Pranav 5.0 13.4 50.0

Bhagya 6.6 18.0 80.0

Gauri 6.0 16.0 1.0

Anand 13.0 13.0 0.0

Bhagya 6.6 18.0 80.0

In [94]: # 7. Store the pre-processed data in an external file named "My_Data.csv" (5)
data1.to_csv("E23MCAG0044.csv")

In [ ]:

You might also like