Advanced Python Practical (Set A + Set B
Programs with Explanation)
Set A - Q1 Dictionary to DataFrame
Program:
import pandas as pd
data = {
"Mobiles": ["Samsung","iPhone","OnePlus","Vivo","Redmi"],
"Price": [20000,70000,35000,18000,15000]
}
df = [Link](data)
print(df)
Explanation:
pandas library is imported.
Dictionary stores mobile names and prices.
DataFrame converts dictionary into table format.
print(df) displays the table.
Set A - Q2 Create CSV and Display DataFrame
Program:
import csv
with open("[Link]","w",newline="") as file:
writer = [Link](file)
[Link](["Employee_Name","Designation","Salary"])
[Link](["Amit","Manager",60000])
[Link](["Rahul","Developer",50000])
[Link](["Sneha","HR",45000])
import pandas as pd
df = pd.read_csv("[Link]")
print(df)
Explanation:
CSV module creates file.
writerow() writes rows.
read_csv() reads CSV file into DataFrame.
Set A - Q3 Display First 5 Rows
Program:
import pandas as pd
data = {
"Name":["Amit","Rahul","Sneha","Amit","Neha","Kiran","Rohit"],
"Age":[21,22,23,24,25,26,27],
"City":["Pune","Mumbai","Delhi","Nagpur","Nashik","Jaipur","Indore"]
}
df = [Link](data)
print([Link]())
Explanation:
DataFrame created from dictionary.
head() displays first 5 rows.
Set A - Q4 Sales DataFrame Operations
Program:
import pandas as pd
data = {
"customer_name":["Aarti","Rahul","Sneha","Amit","Pooja","Kiran","Neha"],
"product":["Laptop","Mobile","Tablet","Headphones","Smartwatch","Laptop","Mobile"],
"quantity":[2,1,3,5,2,1,4],
"price":[50000,20000,15000,2000,8000,52000,18000],
"region":["North","South","North","East","North","West","South"]
}
df = [Link](data)
print([Link])
df["total_amount"] = df["price"] * df["quantity"]
selected = df[["customer_name","product","quantity","price"]]
north_data = df[df["region"]=="North"]
Explanation:
shape returns rows and columns.
New column total_amount is created.
Selected columns display required data.
Filtering shows rows where region is North.
Set B - Q1 IPL DataFrame
Program:
import pandas as pd
import numpy as np
data = {
"Player":["Hardik Pandya","K L Rahul","Andre Russel","Jasprit Bumrah","Virat Kohli","Rohit Sharma"],
"Team":["Mumbai Indians","Kings Eleven","Kolkata Knight Riders","Mumbai Indians","RCB","Mumbai Indian
"Category":["Batsman","Batsman","Batsman","Bowler","Batsman","Batsman"],
"BidPrice":[13,12,7,10,17,15],
"Runs":[1000,2400,900,200,3600,3700]
}
IPL = [Link](data)
print([Link](2))
print([Link](3))
[Link][2,"Runs"] = [Link]
[Link][4,"BidPrice"] = [Link]
most_expensive = [Link][IPL["BidPrice"].idxmax()]
print(most_expensive)
print(IPL["Team"].value_counts())
print(IPL["Runs"].mean())
IPL_clean = [Link]()
Explanation:
head() returns first rows.
tail() returns last rows.
[Link] inserts missing value.
idxmax() finds highest bid price.
value_counts() counts players per team.
mean() calculates average runs.
dropna() removes rows with null values.
Set B - Q2 Join Two DataFrames
Program:
import pandas as pd
student_data1 = [Link]({
"Id":["S2","S3","S4","S5","S5"],
"Name":["Ryder Storey","Bryce Jensen","Ed Bernal","Kwame Morin","Kwame Morin"],
"Marks":[210,190,222,199,199]
})
student_data2 = [Link]({
"Id":["S4","S5","S6","S7","S8"],
"Name":["Scarlette Fisher","Carla Williamson","Dante Morse","Kaiser William","Madeeha Preston"],
"Marks":[201,200,198,219,201]
})
joined_df = [Link](student_data1, student_data2, on="Id", how="inner")
print(joined_df)
Explanation:
[Link]() joins two DataFrames.
on='Id' joins using Id column.
how='inner' returns matching records from both tables.