Record Book Programs 2024-2025
Record Book Programs 2024-2025
HANDLING
WITH PANDAS
#1 Create a series that stores the prices of 8 items. Write the code to find out the four expensive
and non-expensive items from the series.
import pandas as pd
price=pd.Series([340,50,100,290,750,600,430,160])
price=price.sort_values()
print('Four expensive items:')
print(price.tail(4))
print('Four non-expensive items:')
print(price.head(4))
Output
Four expensive items:
0 340
6 430
5 600
4 750
dtype: int64
Four non-expensive items:
1 50
2 100
7 160
3 290
dtype: int64
#2 Create a series and perform slicing operations with loc and iloc operators.
import pandas as pd
data=pd.Series([54,78,21,30,63,87,91,60,49,50])
print('Elements from 4th to 9th positions:')
print(data.loc[3:8])
print('Elements from 4th to 8th positions:')
print(data.iloc[3:8])
Output
Elements from 4th to 9th positions:
3 30
4 63
5 87
6 91
7 60
8 49
dtype: int64
Elements from 4th to 8th positions:
3 30
4 63
5 87
6 91
7 60
dtype: int64
#5 Create a DataFrame from a dictionary of names, salaries and bonus amounts of 5 employees.
Print the highest, lowest and total values from the DataFrame.
import pandas as pd
emp=pd.DataFrame({'Name':['Mia','Samir','Jay','Thara','Kian'],
'Salary':[10000,9500,10500,12500,11000],
'Bonus':[5000,4500,4000,6000,4500]},
index=['E1','E2','E3','E4','E5'])
print(emp)
print(emp.max())
print(emp.min())
print(emp.sum())
Output
Name Salary Bonus
E1 Mia 10000 5000
E2 Samir 9500 4500
E3 Jay 10500 4000
E4 Thara 12500 6000
E5 Kian 11000 4500
Name Thara
Salary 12500
Bonus 6000
dtype: object
Name Jay
Salary 9500
Bonus 4000
dtype: object
Name MiaSamirJayTharaKian
Salary 53500
Bonus 24000
dtype: object
#6 Create a DataFrame to store Item_Number, Name and Price of six items. Print the DataFrame
and its transpose.
import pandas as pd
item=pd.DataFrame({'Item_number':['I01','I02','I03','I04','I05','I06'],
'Name':['Watch','Mobile','Camera','Laptop','Notebook','Router'],
'Price':[16000,5000,2000,5600,6000,900]},
index=['A','B','C','D','E','F'])
print(item)
print(item.T)
Output
Item_number Name Price
A I01 Watch 16000
B I02 Mobile 5000
C I03 Camera 2000
D I04 Laptop 5600
E I05 Notebook 6000
F I06 Router 900
A B C D E F
Item_number I01 I02 I03 I04 I05 I06
Name Watch Mobile Camera Laptop Notebook Router
Price 16000 5000 2000 5600 6000 900
#7 Create a DataFrame of names and capitals of 6 countries. Write the code to add a column
Ranking and add values to it. Delete the column Capital.
import pandas as pd
cdf=pd.DataFrame({'Country':['Thailand','S.Korea',China','Japan','Singapore','Malaysia'],
'Capital':['Bangkok','Seoul', ‘Beijing','Tokyo','Singapore','Kuala Lumpur']})
cdf['Ranking']=[1,2,1,3,1,3]
print(cdf)
cdf=cdf.drop('Capital',axis=1)
print(cdf)
Output
Country Capital Ranking
0 Thailand Bangkok 1
1 S.Korea Seoul 2
2 China Beijing 1
3 Japan Tokyo 3
4 Singapore Singapore 1
5 Malaysia Kuala Lumpur 3
Country Ranking
0 Thailand 1
1 S.Korea 2
2 China 1
3 Japan 3
4 Singapore 1
5 Malaysia 3
#8 Create a DataFrame of rollno, name and grade of 5 students and perform iteration of rows and
columns.
import pandas as pd
student=pd.DataFrame({'Rollno':[1,2,3,4,5],
'Name':['Ahmed','Jemma','John','Somi','Moni'],
'Grade':[12,12,11,12,11]},
index=['S1','S2','S3','S4','S5'])
print('Iteration of rows')
for i,j in student.iterrows():
print(i,j)
print('Iteration of columns')
for i,j in student.iteritems():
print(i,j)
Output
Iteration of rows
S1 Rollno 1
Name Ahmed
Grade 12
Name: S1, dtype: object
...........(all rows)
Iteration of columns
Rollno
S1 1
S2 2
S3 3
S4 4
S5 5
Name:Rollno, dtype:int64
.............(all columns)
#9 Create a DataFrame of Bookcode, Title, Price and Category of 5 books. Write the code to add
a new row, delete the 3rd row and rename the column Category as Genre.
import pandas as pd
book=pd.DataFrame({'Bookcode':['B1','B2','B3','B4','B5'],
'Title':['Peace','Sky','Moon','Star','Fire'],
'Price':[150,200,175,500,430],
'Category':['Anime','Cartoon','Manga','Anime','Manga']})
book.loc[5]=['B6','Sea',200,'Cartoon']
book=book.drop(2,axis=0)
book.rename({‘Category’: ‘Genre’},axis=1,inplace=True)
print(book)
Output
Bookcode Title Price Genre
0 B1 Peace 150 Anime
1 B2 Sky 200 Cartoon
3 B4 Star 500 Anime
4 B5 Fire 430 Manga
5 B6 Sea 200 Cartoon
#10 Create a DataFrame with a list of list that contains code, name, classes and fee for 3
activities. Rename the rows and columns with proper labels.
import pandas as pd
act=pd.DataFrame([[101,'Music',10,200],[102,'Drama',8,150],[103,'Violin',10,250]])
act=act.rename({0:'Activity 1',1:'Activity 2',2:'Activity 3'},axis=0)
act=act.rename({0:'Code',1:'Name',2:'Classes',3:'Fee'},axis=1)
print(act)
Output
Code Name Classes Fee
Activity 1 101 Music 10 200
Activity 2 102 Drama 8 150
Activity 3 103 Violin 10 250
#11 Create a DataFrame with daynumber, dayname and temperature for 5 days. Display the
rows in the alphabetical order of daynames and descending order of temperatures.
import pandas as pd
temp=pd.DataFrame({'dayno':[1,2,3,4,5],
'dayname':['monday','tuesday','wednesday','thursday','friday'],
'temp':[39.8,40,36.9,41,37.5]})
print(temp.sort_values(by='dayname'))
print(temp.sort_values(by='temp',ascending=False))
Output
dayno dayname temp
4 5 friday 37.5
0 1 monday 39.8
3 4 thursday 41.0
1 2 tuesday 40.0
2 3 wednesday 36.9
dayno dayname temp
3 4 thursday 41.0
1 2 tuesday 40.0
0 1 monday 39.8
4 5 friday 37.5
2 3 wednesday 36.9
#12 Create a DataFrame of customerid, cname and amount_due of 3 customers and display the
rows using Boolean indexing.
import pandas as pd
cdf=pd.DataFrame({'customer_id':['C01','C02','C03'],
'cname':['Jay','Ren','Ben'],
'amount_due':[10000,8500,5000]})
print(cdf[cdf.amount_due<7000])
Output
customer_id cname amount_due
2 C03 Ben 5000
#13 Create a DataFrame of name and age of 5 people. Write the code to display the data by slicing
with label indexing.
import pandas as pd
data=pd.DataFrame({'name':['Aman','Dean','Soni','Ray','Zom'],
'age':[20,22,21,25,24]},
index=['p1','p2','p3','p4','p5'])
print(data['p2':'p4'])
Output
name age
p2 Dean 22
p3 Soni 21
p4 Ray 25
#15 Create a DataFrame which contains ProductID, PName and Price of 5 products. Create a csv
file using this DataFrame.
import pandas as pd
pdf=pd.DataFrame({'ProductId':[101,102,103,104,105],
'Pname':['Shoes','Watch','Goggles',
'Clock','Boots'],
'Price':[400,800,450,100,250]})
pdf.to_csv('C:\\Desktop\\items.csv')
'''csv file created'''
DATA
VISUALIZATION
#1 Plot a line chart for the Sales done by a company in 8 months.
import matplotlib.pyplot as plt
sales=(25000,400000,300000,500000,650000,450000,600000,800000)
months=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug")
plt.plot(months,sales,linestyle='dotted',color='maroon',marker='*')
plt.title("Sales Report")
plt.xlabel("Months")
plt.ylabel("Sales Amoount")
plt.show()
#3 Plot a horizontal bar graph from sales.csv that stores the daily sales of a shop for 3 weeks.
import matplotlib.pyplot as plt
import pandas as pd
sdf=pd.read_csv('C:\\Users\\USER 5\\Desktop\\sales.csv')
sdf.plot(kind='barh',x='Day',color=['g','m','y'])
plt.title('Weekly sales')
plt.xlabel('Sales')
plt.ylabel('Days')
plt.show()
#4 Plot a histogram for the columns price and stock from item.csv. Display the histogram
horizontally and with patterns.
import matplotlib.pyplot as plt
import pandas as pd
idf=pd.read_csv('C:\\Users\\USER 5\\Desktop\\item.csv')
idf.plot(kind='hist',hatch='X')
plt.title('Stock Report')
plt.xlabel('Range')
plt.ylabel('Values')
plt.show()