0% found this document useful (0 votes)
13 views

Assignment 7

Ggtgyffgfrgvsbnhh
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)
13 views

Assignment 7

Ggtgyffgfrgvsbnhh
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/ 1

Assignment No:07

Name:Bhosale Sakshi Dattatray

Batch No:02

Numpy LIB
1)Create an array of all even integers from 10-50.

In [6]: import numpy as np


even_integers = [i for i in range(10, 51) if i % 2 == 0]
print(even_integers)

[10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]

2)Create an array of 20 linearly spaced points between 0 and 1.

In [9]: import numpy as np

linear_points = np.linspace(0, 1, 20)


print(linear_points)

[0. 0.05263158 0.10526316 0.15789474 0.21052632 0.26315789


0.31578947 0.36842105 0.42105263 0.47368421 0.52631579 0.57894737
0.63157895 0.68421053 0.73684211 0.78947368 0.84210526 0.89473684
0.94736842 1. ]

3)Write a NumPy program to compute the cross product of two given vectors.

In [12]: import numpy as np


vector_a = np.array([1, 2, 3])
vector_b = np.array([4, 5, 6])
cross_product = np.cross(vector_a, vector_b)

print("Cross product:", cross_product)

Cross product: [-3 6 -3]

4)Write a NumPy program to generate six random integers between 10 and 30.

In [15]: import numpy as np


random_integers = np.random.randint(10, 31, size=6)
print("Random integers:", random_integers)

Random integers: [25 18 16 19 25 15]

5)Write a python program to add two numpy arrays.

In [20]: import numpy as np

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


array2 = np.array([4, 5, 6])
result = array1 + array2
print("Result of addition:", result)

Result of addition: [5 7 9]

6)Write a python program to demonstrate indexing in NumPy array.

In [23]: import numpy as np


array = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
element = array[1, 2]
row = array[0]
column = array[:, 1]
print("Original Array:\n", array)
print("Element at row 1, column 2:", element)
print("First row:", row)
print("Second column:", column)

Original Array:
[[10 20 30]
[40 50 60]
[70 80 90]]
Element at row 1, column 2: 60
First row: [10 20 30]
Second column: [20 50 80]

In [ ]:

Pandas LIB
1)Create a data frame using df = pd.DataFrame(randn(5,4),index='A B C D E'.split(),columns='W X Y Z'.split()) then add a new column to it.

In [28]: import pandas as pd


import numpy as np

df = pd.DataFrame(np.random.randn(5, 4), index='A B C D E'.split(), columns='W X Y Z'.split())

print("Original DataFrame:\n", df)


df['New_Column'] = np.random.randn(5)
print("\nUpdated DataFrame with new column:\n", df)

Original DataFrame:
W X Y Z
A 0.107051 -0.562607 -1.336292 0.310723
B 0.771938 0.248327 0.765860 0.398810
C 0.897134 -1.153312 0.692311 -0.382005
D -1.043636 0.424055 0.570450 -1.049789
E -0.617916 -0.381469 0.651883 0.133204

Updated DataFrame with new column:


W X Y Z New_Column
A 0.107051 -0.562607 -1.336292 0.310723 -0.041982
B 0.771938 0.248327 0.765860 0.398810 -0.118373
C 0.897134 -1.153312 0.692311 -0.382005 0.046169
D -1.043636 0.424055 0.570450 -1.049789 -1.364918
E -0.617916 -0.381469 0.651883 0.133204 1.293948

2)Develop a dataframe for data = {'Company':['GOOG','GOOG','MSFT','MSFT','FB','FB'], 'Person':['Sam','Charlie','Amy','Vanessa','Carl','Sarah'], 'Sales':[200,120,340,124,243,350]}.

In [31]: import pandas as pd

data = {
'Company': ['GOOG', 'GOOG', 'MSFT', 'MSFT', 'FB', 'FB'],
'Person': ['Sam', 'Charlie', 'Amy', 'Vanessa', 'Carl', 'Sarah'],
'Sales': [200, 120, 340, 124, 243, 350]
}
df = pd.DataFrame(data)
print(df)

Company Person Sales


0 GOOG Sam 200
1 GOOG Charlie 120
2 MSFT Amy 340
3 MSFT Vanessa 124
4 FB Carl 243
5 FB Sarah 350

3)Write a Pandas program to remove repetitive characters from the specified column of a given DataFrame.

In [37]: import pandas as pd

data = {
'Company': ['GOOG', 'GOOG', 'MSFT', 'MSFT', 'FB', 'FB'],
'Person': ['Saaam', 'Chaarlie', 'Ammmy', 'Vaanessa', 'Caarl', 'Saaarah'],
'Sales': [200, 120, 340, 124, 243, 350]
}

df = pd.DataFrame(data)
def remove_repetitive_chars(text):
result = []
for char in text:
if not result or result[-1] != char:
result.append(char)
return ''.join(result)

df['Person'] = df['Person'].apply(remove_repetitive_chars)

print(df)

Company Person Sales


0 GOOG Sam 200
1 GOOG Charlie 120
2 MSFT Amy 340
3 MSFT Vanesa 124
4 FB Carl 243
5 FB Sarah 350

4)Write a Pandas program to create a Pivot table and find the total sale amount region wise, manager wise, sales man wise.

In [40]: import pandas as pd


data = {
'Region': ['East', 'East', 'West', 'West', 'North', 'North', 'South', 'South'],
'Manager': ['M1', 'M1', 'M2', 'M2', 'M3', 'M3', 'M4', 'M4'],
'Salesman': ['S1', 'S2', 'S1', 'S3', 'S2', 'S4', 'S3', 'S4'],
'Sales': [200, 150, 300, 400, 250, 150, 100, 200]
}
df = pd.DataFrame(data)

pivot_table = pd.pivot_table(df, values='Sales', index=['Region', 'Manager', 'Salesman'], aggfunc='sum')

print(pivot_table)

Sales
Region Manager Salesman
East M1 S1 200
S2 150
North M3 S2 250
S4 150
South M4 S3 100
S4 200
West M2 S1 300
S3 400

5)Write a python program to create a data frame from 2 dictionaries.

In [43]: import pandas as pd

data1 = {
'Name': ['Sam', 'Charlie', 'Amy'],
'Age': [28, 34, 22]
}

data2 = {
'Company': ['GOOG', 'MSFT', 'FB'],
'Salary': [120000, 95000, 85000]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
combined_df = pd.concat([df1, df2], axis=1)
print(combined_df)

Name Age Company Salary


0 Sam 28 GOOG 120000
1 Charlie 34 MSFT 95000
2 Amy 22 FB 85000

6)Create an employee register using dataframe object in pandas, with "EmpID","EmpName","EmpSalary", "EmpDesignation" as indexes. Add values to the employee register using append() method.

In [ ]: import pandas as pd
employee_register = pd.DataFrame(columns=["EmpID", "EmpName", "EmpSalary", "EmpDesignation"])
def add_employee(emp_id, emp_name, emp_salary, emp_designation):
global employee_register
new_employee = pd.DataFrame({
"EmpID": [emp_id],
"EmpName": [emp_name],
"EmpSalary": [emp_salary],
"EmpDesignation": [emp_designation]
})
employee_register = employee_register.append(new_employee, ignore_index=True)

add_employee(1, "John Doe", 50000, "Software Engineer")


add_employee(2, "Jane Smith", 60000, "Data Scientist")
add_employee(3, "Mike Johnson", 55000, "Project Manager")
print(employee_register)

In [ ]: EmpID EmpName EmpSalary EmpDesignation


0 1 John Doe 50000 Software Engineer
1 2 Jane Smith 60000 Data Scientist
2 3 Mike Johnson 55000 Project Manager

Graded Assignments
1)Write a NumPy program to calculate the difference between the maximum and the minimum values of a given array along the second axis

In [54]: import numpy as np


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

difference = np.max(array, axis=1) - np.min(array, axis=1)

print("Difference between max and min values along the second axis:", difference)

Difference between max and min values along the second axis: [2 2 2]

2)Write a Pandas program to get a time series with the last working days of each month of a specific year.

In [57]: import pandas as pd


import numpy as np
year = 2023
date_range = pd.date_range(start=f'{year}-01-01', end=f'{year}-12-31', freq='B')
last_working_days = date_range[date_range.to_series().dt.is_month_end]
print("Last working days of each month in", year)
print(last_working_days)

Last working days of each month in 2023


DatetimeIndex(['2023-01-31', '2023-02-28', '2023-03-31', '2023-05-31',
'2023-06-30', '2023-07-31', '2023-08-31', '2023-10-31',
'2023-11-30'],
dtype='datetime64[ns]', freq=None)

In [ ]:

You might also like