Practical
Practical
SERIES
# Create a Series
data = {"Amit": 76, "Rita": 82, "John": 65, "Sara": 89, "Rohan": 74}
percentages = pd.Series(data)
2. To write a Python program to create a Series object that stores the Initial budget
allocated (50000/- each) for the four quarters of the year: Qtr1, Qtr2, Qtr3 and Qtr4.
import pandas as pd
4. To Write a Python program to create a Series object with Employee names as the
index and their salaries as values. Accept the name of the employee whose salary needs
to be changed, along with the new salary, and update it in the Series.
import pandas as pd
6. To write a Python program to create two series i.e. population and average income of
Four Zones, and then calculate per capita income storing in third pandas series print it.
import pandas as pd
7. To write a Python program to create a Series using list and display the following
attributes of the Series: (i) index (ii) dtype (iii) size (iv) shape (v) hasnans
import pandas as pd
DATAFRAME
9. To write a Python program to create a panda’s Dataframe for the following table
Using Nested list
import pandas as pd
# Create a nested list for the data
data = [
["Stu1", "Arun", 21],
["Stu2", "Bala", 23],
["Stu3", "Charan", 22],
]
# Create a DataFrame
df = pd.DataFrame(data, columns=columns)
10. To write a Python program to create a panda’s DataFrame called DF for the
following table Using Dictionary of List and perform the following operations:
# (iii) Display the columns 'Books' and 'Uniform' for 'M.P' and 'U.P'
print("\n'Books' and 'Uniform' for 'M.P' and 'U.P':")
print(DF.loc[["M.P", "U.P"], ["Books", "Uniform"]])
(i) Insert a new column “Bags” with values as [5891, 8628, 9785, 4475].
(ii) Delete the row details of M.P from DataFrame DF.
import pandas as pd
# Create a DataFrame
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
import pandas as pd
14. To write a Python program to create a DataFrame using Dictionary of list and
display the following attributes of the DataFrame: (i) index (ii) columns (iii) axes (iv)
dtypes (v) shape (vi) dimension (vii) T
import pandas as pd
# Create a DataFrame
df = pd.DataFrame(data)
print("\n(ii) Columns:")
print(df.columns)
print("\n(iii) Axes:")
print(df.axes)
print("\n(iv) Dtypes:")
print(df.dtypes)
print("\n(v) Shape:")
print(df.shape)
print("\n(vi) Dimension:")
print(df.ndim)
students_df = pd.DataFrame(data)
16. To Write a Python program to store the details of Employess’ such as Empno,
Name, Salary into a Employee.csv file. Also, write a code to read employee details from
csv file.
import pandas as pd
# Creating DataFrame
employees_df = pd.DataFrame(employee_data)
17. To write a Python program to plot a Line chart to depict the changing weekly Onion
and Brinjal prices for four weeks. Also, give appropriate axes labels, title and keep
marker style as Diamond and marker edge color as ‘red’ for Onion.
import matplotlib.pyplot as plt
# Data for the prices of Onion and Brinjal for four weeks
weeks = ['Week 1', 'Week 2', 'Week 3', 'Week 4']
onion_prices = [40, 42, 45, 48]
brinjal_prices = [30, 35, 37, 40]
# Display legend
plt.legend()
# Show the plot
plt.show()
18. To write a Python program to create a DataFrame for subject-wise average, save it
to a CSV file, and then draw a bar chart using Matplotlib with a width of each bar as
0.25, specifying different colors for each bar. Additionally, provide a proper title and
axes labels for the bar chart.
import pandas as pd
import matplotlib.pyplot as plt
# Create DataFrame
df = pd.DataFrame(data)
19. To write a Python program to plot a multiple bar chart From CSV file using
Matplotlib for subject wise Scores of Class A, Class B, and Class C. Different colors
represent each class, and subjects include English,Accountancy,Economics,BST and
IP. Proper labels, a title and a legend are displayed on the chart.
import pandas as pd
import matplotlib.pyplot as plt
21. With reference to the following relations (tables) EMPLOYEE and JOB complete all
the queries in your practical file.
Create following tables EMPLOYEE and JOB such that Empno and Sno are not null
and unique, name is never blank, Area and Native place is valid, hobby, dept is not
empty.
Table: EMPLOYEE
1. Show empno, name and salary of those who have Sports as hobby.
SELECT Empno, Name, Salary
FROM EMPLOYEE
WHERE Hobby = 'Sports';
7. Show the appointment date and native place of those whose name starts with ‘A’ or
ends in ‘d’.
SELECT App_date, Native_place
FROM JOB
WHERE Name LIKE 'A%' OR Name LIKE '%d';
8. Show the salary expense with suitable column heading of those who shall retire after
20-jan-2006.
SELECT SUM(Salary) AS Salary_Expense
FROM JOB
WHERE Retd_date > '2006-01-20';
9. Show an additional burden on the company in case the salary of employees having a
hobby as sports, is increased by 10%.
SELECT SUM(Salary * 0.10) AS Additional_Burden
FROM EMPLOYEE
WHERE Hobby = 'Sports';
11. Show how many employee shall retire today if maximum length of service is 20
Years.
SELECT COUNT(Empno)
FROM JOB
WHERE DATEDIFF(CURDATE(), App_date) / 365 >= 20;
12. Show those employee name and date of birth who have served more than 17 years
as on date.
SELECT Name, DOB
FROM EMPLOYEE
WHERE DATEDIFF(CURDATE(), App_date) / 365 > 17;
13. Show names of those who earn more than all of the employees of Sales dept.
SELECT Name
FROM EMPLOYEE
WHERE Salary > ALL (SELECT Salary FROM JOB WHERE Dept =
'Sales');
14. Show names of those who earn more than at least one of the employees of
Marketing dept.
SELECT Name
FROM EMPLOYEE
WHERE Salary > ANY (SELECT Salary FROM JOB WHERE Dept =
'Marketing');
15. Increase salary of the employees by 5 % of their present salary with hobby as Music
or they have completed at least 3 years of service.
UPDATE EMPLOYEE
SET Salary = Salary * 1.05
WHERE Hobby = 'Music' OR DATEDIFF(CURDATE(), App_date) / 365
>= 3;
16. Show the maximum salary and the hobby of the employee.
SELECT MAX(Salary), Hobby
FROM EMPLOYEE
GROUP BY Hobby;
18. To display the maximum, minimum, sum and average salary of each department.
SELECT Dept, MAX(Salary), MIN(Salary), SUM(Salary),
AVG(Salary)
FROM JOB
GROUP BY Dept;
20. To display employees name, salary and Area of all the employees working in Agra
SELECT Name, Salary, Area
FROM EMPLOYEE
WHERE Area = 'Agra';
21. Add a new tuple(row) in the table essentially with a hobby as Music.
INSERT INTO EMPLOYEE (Empno, Name, DOB, Native_place, Hobby)
VALUES (130, 'NEW_EMPLOYEE', '1990-01-01', 'Delhi',
'Music');
23. Create a table with values of columns empno, name, and hobby.
CREATE TABLE EMPLOYEE_HOBBY (
Empno INT PRIMARY KEY,
Name VARCHAR(100),
Hobby VARCHAR(50)
);
24. Erase the records of employees from the job table whose hobby is not Sports.
DELETE FROM JOB
WHERE Empno IN (SELECT Empno FROM EMPLOYEE WHERE Hobby !=
'Sports');