Assignment 7
Assignment 7
Batch No:02
Numpy LIB
1)Create an array of all even integers from 10-50.
[10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]
3)Write a NumPy program to compute the cross product of two given vectors.
4)Write a NumPy program to generate six random integers between 10 and 30.
Result of addition: [5 7 9]
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.
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
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)
3)Write a Pandas program to remove repetitive characters from the specified column of a given DataFrame.
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)
4)Write a Pandas program to create a Pivot table and find the total sale amount region wise, manager wise, sales man wise.
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
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)
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)
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
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 [ ]: