0% found this document useful (0 votes)
18 views13 pages

Exercises

Uploaded by

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

Exercises

Uploaded by

Nivedika Namburi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

NUMPY ARRAY

EXERCISES
EXERCISE 1: CREATE A 5X2 INTEGER ARRAY FROM A RANGE BETWEEN
100 TO 200 SUCH THAT THE DIFFERENCE BETWEEN EACH ELEMENT IS 10
SOLUTION -1

• import numpy
• print("Creating 5X2 array using numpy.arange")
• sampleArray = numpy.arange(100, 200, 10)
• sampleArray = sampleArray.reshape(5,2)
• print (sampleArray)
EXERCISE 2: FOLLOWING IS THE PROVIDED NUMPY ARRAY. RETURN
ARRAY OF ITEMS BY TAKING THE THIRD COLUMN FROM ALL ROWS

• sampleArray = numpy.array([[11 ,22, 33], [44, 55, 66], [77, 88, 99]])
SOLUTION -2

• import numpy as np

• sampleArray = np.array([[11, 22, 33], [44, 55, 66], [77, 88, 99]])

• # Extract the third column


• third_column = sampleArray[:, 2]

• print(third_column)
EXERCISE 3: RETURN ARRAY OF ODD ROWS AND EVEN
COLUMNS FROM BELOW NUMPY ARRAY

• sampleArray = numpy.array([[3 ,6, 9, 12], [15 ,18, 21, 24], [27 ,30, 33,
36], [39 ,42, 45, 48], [51 ,54, 57, 60]])
SOLUTION-3

• import numpy
• sampleArray = numpy.array([[3 ,6, 9, 12], [15 ,18, 21, 24],
• [27 ,30, 33, 36], [39 ,42, 45, 48], [51 ,54, 57, 60]])
• print("Printing Input Array")
• print(sampleArray)
• print("\n Printing array of odd rows and even columns")
• newArray = sampleArray[::2, 1::2]
• print(newArray)
EXERCISE 4: SPLIT THE ARRAY INTO FOUR EQUAL-SIZED
SUB-ARRAYS

• Note: Create an 8X3 integer


array from a range between 10
to 34 such that the difference
between each element is 1 and
then Split the array into four
equal-sized sub-arrays.
SOLUTION -4

• import numpy
• print("Creating 8X3 array using numpy.arange")
• sampleArray = numpy.arange(10, 34, 1)
• sampleArray = sampleArray.reshape(8,3)
• print (sampleArray)
• print("\nDividing 8X3 array into 4 sub array\n")
• subArrays = numpy.split(sampleArray, 4)
• print(subArrays)
PANDAS EXERCISES
EXERCISE 1. FROM THE GIVEN DATASET PRINT THE FIRST
AND LAST FIVE ROWS

• import pandas as pd
• df = pd.read_csv("Automobile_data .csv")
• df.head(5)
• Df.tail()

Data set - Automobile_data set


EXERCISE 2. FIND THE MOST EXPENSIVE CAR COMPANY
NAME

• import pandas as pd
• df = pd.read_csv("Automobile_data.csv ")
• df = df [['company','price']][df.price==df['price'].max()]
• df
EXERCISE :3 COUNT TOTAL CARS PER
COMPANY
• import pandas as pd
• df = pd.read_csv("Automobile_data.csv")
• df['company'].value_counts()

You might also like