Practical File Sai Lalit
Practical File Sai Lalit
Submitted By
Name: Kakumani Sai Lalit Chandra
Reg. No:
CERTIFICATE
Principal
Contents
Program 1
Question: Write a program to create a Series object using (a) Scalar
Value (b) List (c) ndarray
Program:
scalar_value = eval(input("Please input a scalar value: "))
Sn = pd.Series(scalar_value)
print(Sn)
list1 = eval(input("Please input a list: "))
Sn = pd.Series(list1)
print(Sn)
list1 = eval(input("Please input a list which is to be converted into an
array: "))
ar1 = np.array(list1)
Sn = pd.Series(ar1)
print(Sn)
Output:
Page 2
Program 2
Question: Write a program to create a Series object using a dictionary
that stores the number of students in each section of class 12 in your
school[Sections: A,B,C,D,E,F,G,H Strength: 24,23,20,22,22,24,10,15]
Program:
dict1 = {'A': 24, 'B': 23, 'C': 20, 'D': 22, 'E': 22, 'F': 24, 'G': 10, 'H': 15}
Sn = pd.Series(dict1)
print(Sn)
Output:
Page 3
Program 3
Question: Write a program to create a Series object that stores the
contribution of each section, as shown below:
Program 4
Question: Write a program to compare the elements of two Series
Program:
dict1 = {0:2, 1:4, 2:6, 3:8, 4:10}
dict2 = {0:1, 1:3, 2:5, 3:7, 4:9}
Sn1 = pd.Series(dict1)
Sn2 = pd.Series(dict2)
print('Sn1>Sn2')
print(Sn1>Sn2)
print('Sn1<Sn2')
print(Sn1<Sn2)
print('Sn1=Sn2')
print(Sn1==Sn2)
Output:
Page 5
Program 5
Question: Write a program to create a Series that contains the marks
of five students in one subject. Print all marks that are greater than
75.
Program:
dict1 = {'Rajesh':70, 'Rick':75, 'Rohit':80, 'Ravi':85, 'Rahul':90}
Sn1 = pd.Series(dict1)
print(Sn1[Sn1>75])
Output:
Page 6
Program 6
Question: Create a Series temp that stores temperature of seven days
in it. Its index should be Sunday, Monday, … Write a program to
a. Display temperature of first 3 days.
b. Display temperature of last 3 days.
c. Display all temperature in reverse order like Saturday, Friday, ...
d. Display square of all temperature
Program:
listtemp = [30, 32, 35, 33, 39, 29, 35]
Sn1 = pd.Series(listtemp, index=
['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'])
print(Sn1.head(3))
print(Sn1.tail(3))
print(Sn1[::-1])
print(Sn1**2)
Output:
Page 7
Program 7
Question: Write a program to create a dataframe quarterly sales
where each row contains the quarterly sales of TV, Fridge and AC.
Show the dataframe after deleting the details of AC.
Program:
dict1 = {'TV': [20000,10000,15000,14500],'Fridge':
[15000,22000,10000,11000],'AC': [25000,12000,9000,19000]}
df1 = pd.DataFrame(dict1,index=['Qtr1','Qtr2','Qtr3','Qtr4'])
del df1['AC']
print(df1)
Output:
Page 8
Program 8
Question: Write a program to create a dataframe to store rollnumber,
name and marks of five students. Print the dataframe, along with the
number of rows, columns and total elements in the dataframe and
also its transpose.
Program:
dict1 = {'Rollnumber': [101,102,103,104,105],'Name':
['Karan','Taran','Piyush','Bhupinder','Simran'],'Marks': [82,86,79,86,94]}
df = pd.DataFrame(dict1)
print(len(df.index))
print(len(df.columns))
print(df.size)
print(df.T)
Output:
Page 9
Program 9
Question: Write a program to create a dataframe which contains
Rollnumber, Name, Height, Weight of 5 students. Add a column
BMI(Body Mass Index) using appropriate formula.
Program:
dict1 ={'Rollnumber':[1,2,3,4,5],'Name':['A','B','C','D','E'],'Height':
[1.65,1.68,1.80,1.74,1.72],'Weight':[72,71,73,71,72]}
df = pd.DataFrame(dict1)
df['BMI'] = df['Weight']/(df['Height']**2)
print(df)
Output:
Page 10
Program 10
Question: Write a program to create a dataframe AID using the data
given below and do the following.
a. Display the books and shoes only
b. Display toys only
c. Display quantity in MP and CG for toys and books. d. Display
quantity of books in AP
Program:
dict1 = {'Toys':
[7000,3400,7800,4100],'Books':
[4300,3200,5600,2000],'Shoes':
[6000,1200,3280,3000]}
AID = pd.DataFrame(dict1,
index=['MP','UP','AP','CG'])
print(AID)
print(AID.loc[:,'Books':'Shoes'])
print(AID.Toys)
print(AID.loc['MP':'CG':3,'Toys':'B
ooks'])
Output:
Page 11
Program 11
Question: Consider two series object Staff and salaries that stores the
number of people in various branches and salaries distributed in
these branches respectively. Write a program to create another
Series object that stores average salary per branch and then create a
dataframe object from these Series object. After creating dataframe
rename all row labels with Branch name.
Program:
staff = pd.Series([15, 50, 20])
salary = pd.Series([1500000, 4000000, 1300000])
avg = pd.Series(salary/staff)
branch = ['IT','Marketing','HR']
df = pd.DataFrame({'Branch':branch,'Staff':staff,'Total
Salary':salary,'Avg_Salary':avg})
print(df)
df.set_index('Branch',inplace=True)
print(df)
Output:
Page 12
Program 12
Question: Write a program to create the dataframe sales containing year wise
sale figure for five sales persons in INR. Use the year as column labels and the
sales person names as row labels.
Program 13
Question: Write a program to create a dataframe Accessories using data as
given below and write program to do the following:
Program:
dict1 = {'Cname': ['Motherboard','Hard
Disk','Keyboard','LCD'],'Quantity':
[15,24,10,30],'Price':
[1200,5000,500,450]}
df = pd.DataFrame(dict1,
index=['C1','C2','C3','C4'])
print(df)
df['Price'] = df['Price']*(110/100)
print(df)
df.rename(index={'C1':'C001','C2':'C002'
,'C3':'C003','C4':'C004'}, inplace=True)
print(df)
df.drop('C003',axis=0,inplace=True)
print(df)
del df['Quantity']
print(df)
Page 14
Program 14
Question: Write a program to create a dataframe using the following
data and store it into a csv file student.csv without row labels and
column labels.
Program:
dict1 = {'Rollnumber': [101,102,103,104,105],'Name':
['Karan','Taran','Piyush','Bhupinder','Simran'],'Marks': [82,86,79,86,94]}
df = pd.DataFrame(dict1)
df.to_csv('D:/file.csv',index=False,header=False)
Output:
Page 15
Program 15
Question: Write a program to read the contents of the csv file
student.csv and display it. Update the row labels and column labels
to be same as that of previous question.
Program:
df = pd.read_csv('D:/student.csv',
names=['Rollnumber','Name','Marks'])
print(df)
Output:
Page 16
Program 16
Question: Collect and store total medals won by 10 countries in
Olympic games and represent it in form of bar chart with title to
compare and analyse data.
Program:
medals = [10,8,15,26,13,2,3,7,6,4]
countries =
['UK','USA','GERMANY','AUSTRALIA','ITALY','SPAIN','FRANCE','SWITZERL
AND','TURKEY','INDIA']
plt.bar(countries, medals, color = 'orange')
plt.title('Medals won in Olympics')
plt.show()
Output:
Page 17
Program 17
Question: Techtipow Automobiles is authorized dealer of different
bike companies. They record the entire sale of bike month wise as
given below.
Output:
Page 19
Program 18
Question: Given the school result data, analyse the student
performance on different parameters, e.g., subject wise or class wise.
Create a dataframe for the above , plot appropriate chart with title
and legend.
Program:
dict1 = {'Eng':[78,89,67,88],'Math':[89,91,95,78],'Phy':
[69,70,96,65],'Chem':[92,85,94,87],'IT':[90,98,90,80]}
df = pd.DataFrame(dict1, index=[9,10,11,12])
df.plot(kind='bar')
plt.show()
Output:
Page 20
Program 19
Question: The following seat bookings are the daily records of a
month from a cinema hall.
124,124,135,156,128,189,200,176,156,145,138,200,178,189,156,124,1
42,156,176,180,199,154, 167,198,143,150,144,152,150,143
Construct a histogram for the above data with 10 bins.
Program:
seats =
[124,124,135,156,128,189,200,176,156,145,138,200,178,189,156,124,
142,156,176,180,199,154,167,198,143,150,144,152,150,143]
nbins = 10
plt.hist(seats,bins=nbins, facecolor = 'pink', edgecolor = 'red', linewidth
= 3)
plt.show()
Output:
Page 21
Program 20
Question: Write a program to create a dataframe for subject wise
average, save it to a CSV file and then draw a bar chart with a width
of each bar as 0.25, specifying different colors for each bars.
Additionally, provide a proper title and axes labels for the bar chart.
Program:
sub_list = ['Eng','Phy','Chem','Maths','IP']
marks_list = [72,85,90,89,100]
dict1 = {'Subject': sub_list,'Avg Marks': marks_list}
df = pd.DataFrame(dict1, index=['Sub1','Sub2','Sub3','Sub4','Sub5'])
print(df)
df.to_csv(r'D:\subject_wise_average_marks.csv')
plt.bar(sub_list,marks_list,width=0.25,color = ['red','blue','green','yellow','pink'])
plt.xlabel('Subjects')
plt.ylabel('Marks')
plt.title('Subject Wise Average Marks')
plt.show()
Page 22
Output:
Page 23
Program 21
Question: To write Queries for the following questions based on the
given table:
N. Display the names and the length of each name from the above
table.
SELECT Name, LENGTH(NAME) FROM Employee;
Page 29