Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

STD XII-IP Ch-1 (Practical)

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

Write a program N-Dimensional array(ndarray) in Numpy

# Python program to demonstrate


# basic array characteristics
import numpy as np

# Creating array object


arr = np.array( [[ 1, 2, 3],
[ 4, 2, 5]] )

# Printing type of arr object


print("Array is of type: ", type(arr))

# Printing array dimensions (axes)


print("No. of dimensions: ", arr.ndim)

# Printing shape of array


print("Shape of array: ", arr.shape)

# Printing size (total number of elements) of array


print("Size of array: ", arr.size)

# Printing type of elements in array


print("Array stores elements of type: ", arr.dtype)

Output :
Array is of type: <class 'numpy.ndarray'>
No. of dimensions: 2
Shape of array: (2, 3)
Size of array: 6
Array stores elements of type: int64

Write a program Numpy Basic operations

# Python program to demonstrate


# basic operations on single array
import numpy as np

a = np.array([1, 2, 5, 3])

# add 1 to every element


print ("Adding 1 to every element:", a+1)

# subtract 3 from each element


print ("Subtracting 3 from each element:", a-3)

# multiply each element by 10


print ("Multiplying each element by 10:", a*10)

# square each element


print ("Squaring each element:", a**2)

Std XII – IP 1
# modify existing array
a *= 2
print ("Doubled each element of original array:", a)

# transpose of array
a = np.array([[1, 2, 3], [3, 4, 5], [9, 6, 0]])

print ("\nOriginal array:\n", a)


print ("Transpose of array:\n", a.T)

Output :
Adding 1 to every element: [2 3 6 4]
Subtracting 3 from each element: [-2 -1 2 0]
Multiplying each element by 10: [10 20 50 30]
Squaring each element: [ 1 4 25 9]
Doubled each element of original array: [ 2 4 10 6]

Original array:
[[1 2 3]
[3 4 5]
[9 6 0]]
Transpose of array:
[[1 3 9]
[2 4 6]
[3 5 0]]

Write a program to Creating a Numpy Array

# Python program for


# Creation of Arrays
import numpy as np

# Creating a rank 1 Array


arr = np.array([1, 2, 3])
print("Array with Rank 1: \n",arr)

# Creating a rank 2 Array


arr = np.array([[1, 2, 3],
[4, 5, 6]])
print("Array with Rank 2: \n", arr)

# Creating an array from tuple


arr = np.array((1, 3, 2))
print("\nArray created using "
"passed tuple:\n", arr)

Std XII – IP 2
Output:
Array with Rank 1:
[1 2 3]
Array with Rank 2:
[[1 2 3]
[4 5 6]]

Array created using passed tuple:


[1 3 2]

Write a program Basic Array Operations

# Python program to demonstrate


# basic operations on single array
import numpy as np

# Defining Array 1
a = np.array([[1, 2],
[3, 4]])

# Defining Array 2
b = np.array([[4, 3],
[2, 1]])

# Adding 1 to every element


print ("Adding 1 to every element:", a + 1)

# Subtracting 2 from each element


print ("\nSubtracting 2 from each element:", b - 2)

# sum of array elements


# Performing Unary operations
print ("\nSum of all array "
"elements: ", a.sum())

# Adding two arrays


# Performing Binary operations
print ("\nArray sum:\n", a + b)

Output:
Adding 1 to every element:
[[2 3]
[4 5]]

Subtracting 2 from each element:

Std XII – IP 3
[[ 2 1]
[ 0 -1]]

Sum of all array elements: 10

Array sum:
[[5 5]
[5 5]]

Write a program Python List Comprehension


and Slicing
[start : stop : steps]

which means that slicing will start from index start


will go up to stop in step of steps.
Default value of start is 0, stop is last index of list
and for step it is 1
So [: stop] will slice list from starting till stop index and [start : ] will slice list from
start index till end Negative value of steps shows right to left traversal instead of left
to right traversal that is why [: : -1] prints list in reverse order.

# Let us first create a list to demonstrate slicing


# lst contains all number from 1 to 10
lst = range(1, 11)
print lst

# below list has numbers from 2 to 5


lst1_5 = lst[1 : 5]
print lst1_5

# below list has numbers from 6 to 8


lst5_8 = lst[5 : 8]
print lst5_8

# below list has numbers from 2 to 10


lst1_ = lst[1 : ]
print lst1_

# below list has numbers from 1 to 5


lst_5 = lst[: 5]
print lst_5

# below list has numbers from 2 to 8 in step 2


lst1_8_2 = lst[1 : 8 : 2]
print lst1_8_2

# below list has numbers from 10 to 1


lst_rev = lst[ : : -1]
print lst_rev

Std XII – IP 4
# below list has numbers from 10 to 6 in step 2
lst_rev_9_5_2 = lst[9 : 4 : -2]
print lst_rev_9_5_2

Output:

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

Write a program Creating a dataframe using List:

# import pandas as pd
import pandas as pd

# list of strings
lst = ['Geeks', 'For', 'Geeks', 'is',
'portal', 'for', 'Geeks']

# Calling DataFrame constructor on list


df = pd.DataFrame(lst)
print(df)

Output:

Std XII – IP 5
Write a program Creating DataFrame from dict of ndarray/lists

# Python code demonstrate creating


# DataFrame from dict narray / lists
# By default addresses.

import pandas as pd

# intialise data of lists.


data = {'Name':['Tom', 'nick', 'krish', 'jack'],
'Age':[20, 21, 19, 18]}

# Create DataFrame
df = pd.DataFrame(data)

# Print the output.


print(df)

OutPut:

# Import pandas package


import pandas as pd

# Define a dictionary containing employee data


data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}

# Convert the dictionary into DataFrame


df = pd.DataFrame(data)

# select two columns


print(df[['Name', 'Qualification']])

Std XII – IP 6
Output:

Write a program Filling missing values using fillna(), replace() and interpolate()
:
# importing pandas as pd
import pandas as pd

# importing numpy as np
import numpy as np

# dictionary of lists
dict = {'First Score':[100, 90, np.nan, 95],
'Second Score': [30, 45, 56, np.nan],
'Third Score':[np.nan, 40, 80, 98]}

# creating a dataframe from dictionary


df = pd.DataFrame(dict)

# filling missing value using fillna()


df.fillna(0)

Output:

Std XII – IP 7

You might also like