Page 309 - Touhpad Ai
P. 309
21. Create a scatter plot showing the relationship between the number of hours studied and the marks obtained by
10 students.
import seaborn as sns
import matplotlib.pyplot as plt
study_hours = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
marks = [40, 45, 60, 55, 48, 65, 67, 90, 85, 76]
sns.set_style("darkgrid")
sns.scatterplot(x=study_hours, y=marks, color='green')
plt.xlabel("Hours Studied")
plt.ylabel("Marks Obtained")
plt.title("Study Hours vs Marks")
plt.show()
22. Create a Python program using Pandas to perform the following data transformation tasks on the given employee
data:
Convert the Joining_Date column from "MM/DD/YYYY" to "YYYY-MM-DD" format.
Calculate the monthly salary from the Annual_Salary column and add it as a new column.
Encode the Department column using numbers (HR = 1, IT = 2, Sales = 3).
Replace missing values in the Bonus column with 0.
(Bonus) Create a column called Experience_Years by calculating years from Joining_Date till today.
import pandas as pd
from datetime import datetime
# Step 1: Create sample data
data = {
'Employee': ['Aarav', 'Elena', 'Kashish', 'Manish', 'Ishaan'],
'Joining_Date': ['05/14/2020', '11/20/2018', '07/03/2019', '01/15/2021',
'12/05/2017'],
'Annual_Salary': [480400, 602070, 550040, 725006, 505006],
'Department': ['HR', 'IT', 'Sales', 'IT', 'HR'],
'Bonus': [20000, None, 15000, None, 10000]
}
df = pd.DataFrame(data)
print("Original DataFrame:")
Practical File 307

