Practical
Practical
8
a. Write a program to generate a series of marks of 10 students. Give grace marks up to 3 marks of those who are having marks between 30 to 33 marks and print the
new list of the marks. (Pandas Series) [2]
B0002 Computer Science Move fast with computer science XII Dhanpat Rai 340
B0006 Informatics Practices CBSE Questions Bank XII Oswal Books 299
import pandas as pd
import matplotlib.pyplot as mpp
#Answer 1
data={'BookID':['B0001','B0002','B0003','B0004','B0005','B0006'],\
'Subject':['Computer Science','Computer Science','Computer Appllications',\
'Informatics Practices','Artificial Intelligence','Informatics Practices'],\
'Class':['XII','XII','X','XII','IX','XII'],\
'Publisher':['NCERT','Dhanpat Rai','BPB','NCERT','KIPS','Oswal books'],\
'Price':[270,340,120,270,340,299]}
books=pd.DataFrame(data)
#Asnwer 2
print("Class XII Books:")
print(books[books['Class']=='XII'].to_string(header=False,index=False))
print("***********************************************************")
#Asnwer 3
print("Books having price more than 250")
print(books[books['Price']>250].to_string(header=False,index=False))
#Answer 4
books.plot(x='Subject',y='Price',kind='bar')
mpp.show()
Ans- data={'BookID':['B0001','B0002','B0003','B0004','B0005','B0006'],\
'Subject':['Computer Science','Computer Science','Computer Appllications',\
'Informatics Practices','Artificial Intelligence','Informatics Practices'],\
'Class':['XII','XII','X','XII','IX','XII'],\
'Publisher':['NCERT','Dhanpat Rai','BPB','NCERT','KIPS','Oswal books'],\
'Price':[270,340,120,270,340,299]}
books=pd.DataFrame(data)
Ans- books.plot(x='Subject',y='Price',kind='bar')
mpp.show()
2. SQL Queries 7
Consider the following table medicine and write answers for given questions below:
medicineID Medicinename Price Manufacturer
>> use company1; # we are entering into the database where we will work
>> create table medicine(medicineID int,Medicinename varchar(120),Price int, Manufacturer varchar(120)); #Table is created with the column name
and datatype
Observe the above medicine table and write queries for following:
Ans.: length(right(medicinename,4))
————————————-
4
4
4
4
4
b) Select instr(price,5) from medicine;
Ans.: instr(price,5)
————————–
2
0
2
2
2
Write a program in python to create the following dataframe named “country” storing the following details:
Considering the above dataframe answer the following question by writing appropriatecommand in python pandas:
import pandas as pd
import matplotlib.pyplot as plt
d={'country':['China','India','US','Indonasia','Brazil','Pakistan'],
'population':[1379750000,1330780000,324882000,260581000,206918000,194754000],
'birthrate':[12.00,21.76,13.21,18.84,18.43,27.62],
'updatedate':['2010-10-01','2010-01-04','2009-03-01','2009-04-01','2008-08-05','2010- 04-05']}
country=pd.DataFrame(d)
print("Answer 1")
print(country[country['country']=='China'].to_string(header=False,index=False))
print(country[country['country']=='India'].to_string(header=False,index=False))
print("Answer 2")
c=country.loc[:,['country','population','birthrate']]
print(c[c['country']=='Brazil'].to_string(header=False,index=False))
print(c[c['country']=='Pakistan'].to_string(header=False,index=False))
plt.bar(country['country'],country['population'],color=['r','g','b','c','m','y','k'])
plt.title("Population Report")
plt.xlabel("Country")
plt.ylabel("Population")
plt.grid()
plt.show()
country.to_csv('country.csv')
Ans- country.to_csv('country.csv')
4. Now plot a bar chart depicting the Country on x-axis and their corresponding Population on y-axis, with appropriate Graph title, x-axis title, y-axis title, gridlines
an color etc.
Ans- plt.bar(country['country'],country['population'],color=['r','g','b','c','m','y','k'])
plt.title("Population Report")
plt.xlabel("Country")
plt.ylabel("Population")
plt.grid()
plt.show()
2. SQL Queries 7
Table – Charity
>>create table charity(item_id int, Itemname char(50),Price int,Qty int,Pdate varchar(99)); # Table is created with the column name and
datatype
vi) Display itemname, price and qty in the descending order of price
Ans- select itemname, price, qty from charity order by price desc;
import pandas as pd
import matplotlib.pyplot as plt
d={'Ename':['Anil','Akshay','Ajay','Varun','Siddharth','Rajesh'],
'Post':['Manager','Clerk','Manager','Analyst','Developer','Clerk'],
'Salary':[65000,33000,75000,66000,60000,35000],
'Dt_join':['2018-03-02','2018-05-01','2018-09-15','2018-04-11','2018-10-12','2018-06-12']}
employee=pd.DataFrame(d,index=[101,102,103,104,105,106])
print("Answer 1")
em=employee.loc[:,['Ename','Post','Salary']]
print(em[em.Salary>60000].to_string(header=False,index=False))
print("Answer 2")
employee.loc[employee.index[-1]+1]=['Ranveer','Analyst',65000,'2020-01-06']
print(employee.iloc[-1])
employee.to_csv('employees.csv')
plt.plot(employee['Ename'],employee['Salary'],color='r')
plt.title("Employee Analysis",color='blue')
plt.xlabel("Employee Names",color='red')
plt.ylabel("Salary",color='magenta')
plt.legend(['Salary'])
plt.show()
Considering the above dataframe answer the following queries by writing appropriate command in python pandas.
i. Display Name, Post and Salary for all employees earning more than 60000 .
Ans- print("Answer 1")
em=employee.loc[:,['Ename','Post','Salary']]
print(em[em.Salary>60000].to_string(header=False,index=False))
iv. Now plot a multi-line chart depicting the Employee Name on x-axis and their corresponding Salary on y-axis, with appropriate Graph title, x-axis title, y-axis title, legends
and color etc.
Ans- plt.plot(employee['Ename'],employee['Salary'],color='r')
plt.title("Employee Analysis",color='blue')
plt.xlabel("Employee Names",color='red')
plt.ylabel("Salary",color='magenta')
plt.legend(['Salary'])
plt.show()
2. SQL Queries 7
Create the table “employee” and write sql queries for the following: