[go: up one dir, main page]

0% found this document useful (0 votes)
16 views3 pages

Assignment 2

The document contains three assignments focusing on Python programming using pandas and numpy. The first assignment demonstrates creating a DataFrame with student data, the second shows matrix creation and summation, and the third combines both libraries for array operations, DataFrame manipulation, and saving to a CSV file. Each section includes code snippets and expected outputs.

Uploaded by

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

Assignment 2

The document contains three assignments focusing on Python programming using pandas and numpy. The first assignment demonstrates creating a DataFrame with student data, the second shows matrix creation and summation, and the third combines both libraries for array operations, DataFrame manipulation, and saving to a CSV file. Each section includes code snippets and expected outputs.

Uploaded by

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

ASSIGNMENT 2

Q.1) Practice Example on pandas- Write the output.

import pandas as pd

# Data for students

data = {

'Name': ['Kedar', 'Bhavesh', 'Charlie', 'David', 'Mohan'],

'Age': [20, 21, 22, 23, 20],

'Marks': [85, 78, 92, 88, 95]

# Creating DataFrame

df = pd.DataFrame(data)

print(df.head())

Output:

Q.2) Practice program on numpy – Write output.

import numpy as np

# Step 1: Create a 3x3 matrix with values from 1 to 9

matrix = np.arange(1, 10).reshape(3, 3)

print("3x3 Matrix:\n", matrix)

# Step 2: Compute the sum of all elements in the matrix

sum_of_elements = np.sum(matrix)

print("\nSum of all elements in the matrix:", sum_of_elements)


Output:

3x3 Matrix:

[[1 2 3]

[4 5 6]

[7 8 9]]

Sum of all elements in the matrix: 45

Q.3) Practice Example on pandas & numpy : Write the output.

# Import NumPy and Pandas

import numpy as np

import pandas as pd

# Declare an array and perform mathematical operations

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

print("Original Array:", arr)

squared_arr = np.square(arr) # Square of each element

mean_value = np.mean(arr) # Mean of the array

print("Squared Array:", squared_arr)

print("Mean Value:", mean_value)

# Create a 2D array (Matrix)

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

print("\nOriginal Matrix:\n", matrix)

# Perform Transpose of the Matrix

transposed_matrix = np.transpose(matrix)

print("\nTransposed Matrix:\n", transposed_matrix)

# Create a DataFrame using Pandas

data = {

"Name": ["Abha", "Bin", "neil"],

"Age": [25, 30, 22],

"Salary": [50000, 60000, 45000]

df = pd.DataFrame(data)
print("\nOriginal DataFrame:\n", df)

# Add a new column to the DataFrame

df["Department"] = ["HR", "IT", "Finance"]

print("\nDataFrame After Adding New Column:\n", df)

# Filter a column of the DataFrame (e.g., selecting only "Salary" column)

salary_column = df["Salary"]

print("\nFiltered Column (Salary):\n", salary_column)

# Save the DataFrame to a CSV file

df.to_csv("employee_data.csv", index=False)

print("\nDataFrame saved as 'employee_data.csv' successfully!")

Output:

You might also like