0% found this document useful (0 votes)
38 views6 pages

Python PB 3 System

The document discusses various NumPy and Pandas programs for working with arrays, matrices, and DataFrames. It includes examples for finding max/min of arrays, array operations like addition/subtraction, reshaping arrays, matrix multiplication, creating DataFrames from lists and dicts, and converting between DataFrames and CSV files.

Uploaded by

Nikhil Sharma
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)
38 views6 pages

Python PB 3 System

The document discusses various NumPy and Pandas programs for working with arrays, matrices, and DataFrames. It includes examples for finding max/min of arrays, array operations like addition/subtraction, reshaping arrays, matrix multiplication, creating DataFrames from lists and dicts, and converting between DataFrames and CSV files.

Uploaded by

Nikhil Sharma
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
You are on page 1/ 6

Python Problem Sheet – 3 Sub Date : 25 October 2023

1. Write the program to get the maximum and minimum value from a given matrix.
Code :

import numpy as np
matrix = np.array([[4, 2, 7],[1, 9, 3],[5, 6, 8]])
max_value = np.max(matrix)
min_value = np.min(matrix)
print("Maximum value:", max_value)
print("Minimum value:", min_value)

Output :
Maximum value: 9
Minimum value: 1

2. Create nDArray with values ranging from 10 to 49 each saved with difference of 3.
Code :

import numpy as np
nDArray = np.arange(10, 49, 3)
print(nDArray)

Output :

[10 13 16 19 22 25 28 31 34 37 40 43 46 49]

3. Find the number of rows and columns of a given matrix using NumPy. Extract First row and
second column.
Code :

import numpy as np
matrix = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
num_rows, num_columns = matrix.shape
first_row = matrix[0, :]
second_column = matrix[:, 1]
print("Number of rows:", num_rows)
print("Number of columns:", num_columns)
print("First row:", first_row)
print("Second column:", second_column)

Output :

Number of rows: 3
Number of columns: 3
First row: [1 2 3]
Second column: [2 5 8]

KD DD
Python Problem Sheet – 3 Sub Date : 25 October 2023

4. Write a program to get Addition, Subtracting and division of two Matrices in Python.
Code :

import numpy as np
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
addition = matrix1 + matrix2
subtraction = matrix1 - matrix2
division = matrix1 / matrix2
print("Addition:\n", addition)
print("Subtraction:\n", subtraction)
print("Division:\n", division)

Output :

Addition:
[[ 6 8]
[10 12]]
Subtraction:
[[-4 -4]
[-4 -4]]
Division:
[[0.2 0.33333333]
[0.42857143 0.5 ]]

5. Write a numpy program to convert 1D Array in to 2D Array with 3 Rows.


Code :

import numpy as np
array1d = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
array2d = array1d.reshape(3, -1)
print(array2d)

Output :

[[1 2 3]
[4 5 6]

KD DD
Python Problem Sheet – 3 Sub Date : 25 October 2023

[7 8 9]]

6. Write a numpy program to create two Array A and B and calculate covariance and correlation
coefficient of two arrays.
Code :

import numpy
A = numpy.array([1, 2, 3, 4, 5])
B = numpy.array([5, 4, 3, 2, 1])
covariance = numpy.cov(A, B)[0, 1]
correlation = numpy.corrcoef(A, B)[0, 1]
print("Covariance:", covariance)
print("Correlation coefficient:", correlation)

Output :

Covariance: -2.5
Correlation coefficient: -0.9999999999999999

7. Matrix Multiplication in NumPy example, for two matrices A and B. A = [[1, 2], [2, 3]] B = [[4,
5], [6, 7]]
Code :

import numpy
A = numpy.array([[1, 2], [2, 3]])
B = numpy.array([[4, 5], [6, 7]])
result = numpy.dot(A, B)
print("Matrix Multiplication result:")
print(result)

Output :

Matrix Multiplication result:


[[16 19]
[26 31]]

8. Make a Pandas DataFrame with two-dimensional list in column.


Code :

import pandas
data = [[1, 'Name'],[2, 'Name2'],[3, 'Name3']]
df = pandas.DataFrame(data, columns=['ID', 'Name'])
print(df)

KD DD
Python Problem Sheet – 3 Sub Date : 25 October 2023

Output :

ID Name
0 1 Name
1 2 Name2
2 3 Name3

9. Write program to Creating DataFrame from dict of narray/lists in column in pandas.


Code :

import pandas
data = {'Name': ['Name', 'Name2', 'Name3'],'Age': [25, 30, 55]}
df = pandas.DataFrame(data)
print(df)

Output :

Name Age
0 Name 25
1 Name2 30
2 Name3 55

10. Write a program to convert csv file in to data frame.


Code :

• Demo.csv
Name,Age
Name,25
Name2,30
Name3,55

• Q10.py

import pandas
df = pandas.read_csv('data.csv')
print(df)

Output :

Name Age

KD DD
Python Problem Sheet – 3 Sub Date : 25 October 2023

0 Name 25
1 Name2 30
2 Name3 55

11. Write a program to convert data frame to csv file.


Code :

import pandas
data = {'Name': ['Name', 'Name2', 'Name5'],'Age': [25, 30, 55]}
df = pandas.DataFrame(data)
df.to_csv('data.csv', index=False)

Output :

• Demo.csv
Name,Age
Name,25
Name2,30
Name5,55

12. Write a program to create following data frame and find out first 3 and last 3 rows form that.
Code :

import pandas as pd

# Define the data


data = {
'e_no': [1, 2, 3, 4, 5],
'lname': ['Hurwood', 'Kerin', 'Gross', 'Wilson', 'Chung'],
'fname': ['Roger', 'Linda', 'Mary', 'Arthur', 'Yung'],
'Street': ['1234 Stirrup La', '802 Wilderness Dr', '303 Stagecoach Rd', '211 Main St',
'422 Maple St'],
'City': ['Green Valley', 'Wild Woods', 'Green Valley', 'Yellow Fountain', 'Garden
City'],
'st': ['MD', 'VA', 'MD', 'VA', 'MD'],
'zip': [20441, 33256, 20441, 33210, 20331],
'dept': [2020, 2020, 2020, 2020, 1050],
'payrate': [16.00, 17.50, 10.00, 17.50, 12.25]
}

# Create a DataFrame
df = pd.DataFrame(data)

# Display the first 3 rows


first_3_rows = df.head(3)

KD DD
Python Problem Sheet – 3 Sub Date : 25 October 2023

print("First 3 rows:")
print(first_3_rows)

# Display the last 3 rows


last_3_rows = df.tail(3)
print("\nLast 3 rows:")
print(last_3_rows)

Output :

First 3 rows:
e_no lname fname Street City st zip dept payrate
0 1 Hurwood Roger 1234 Stirrup La Green Valley MD 20441 2020 16.0
1 2 Kerin Linda 802 Wilderness Dr Wild Woods VA 33256 2020 17.5
2 3 Gross Mary 303 Stagecoach Rd Green Valley MD 20441 2020 10.0

Last 3 rows:
e_no lname fname Street City st zip dept payrate
2 3 Gross Mary 303 Stagecoach Rd Green Valley MD 20441 2020 10.00
3 4 Wilson Arthur 211 Main St Yellow Fountain VA 33210 2020 17.50
4 5 Chung Yung 422 Maple St Garden City MD 20331 1050 12.25

KD DD

You might also like