Python Practical File 12
Python Practical File 12
Solution:
import pandas as pd
std_marks = []
for i in range(1,11):
std_marks.append(m)
s = pd.Series(index=range(1201,1211),data=std_marks)
s[s==32]=s+1
s[s==31]=s+2
s[s==30]=s+3
print(s[s>=33])
Pandas Dataframe
I have taken question from pandas dataframe for 3 marks. Here
Move fast
Computer with Dhanpat
B0002 XII 340
Science computer Rai
science
Computer Sample
B0003 X BPB 120
Applications Papers
NCERT
Informatics
B0004 Computer XII NCERT 270
Practices
Science
Artificial Artificial
B0005 IX KIPS 340
Intelligence Intelligence
CBSE
Informatics Oswal
B0006 Questions XII 299
Practices Books
Bank
import pandas as pd
#Answer 1
data={'BookID':['B0001','B0002','B0003','B0004','B0005','B0006'],\
'Class':['XII','XII','X','XII','IX','XII'],\
'Price':[270,340,120,270,340,299]}
books=pd.DataFrame(data)
#Asnwer 2
print(books[books['Class']=='XII'].to_string(header=False,index=False))
print("***********************************************************")
#Asnwer 3
print(books[books['Price']>250].to_string(header=False,index=False))
#Answer 4
books.plot(x='Subject',y='Price',kind='bar')
mpp.show()
MySQL
Consider the following table and write answers for given questions below:
RollNo Name Class DOB Gender City Marks
Solution:
1 Create table:
create table students
class varchar(4),
dob date,
gender char(1),
city varchar(20),
marks float);
2 Insert records:
3 display the detail of class XII students in descending order of their marks
6 Count the students class wise is display only those number who is more
than 2
Python Program
Q 1 Write a program in python to create the following dataframe named
“country” storing the following details:
Considering the above dataframe answer the following queries by writing appropriate
command in python pandas.
i. Add a new column named discount which is 10% of their bill amount.
ii. Add a row with row index 1008 as Rohan,Bharuch,6000,2011-04-01.
iii. Now plot a bar chart depicting the customer name on x-axis and their
corresponding
bill amount on y-axis, with appropriate Graph title, x-axis title, y-axis title, gridlines
and color etc.
Solution:
import pandas as pd
'city':['Ahmedabad','Baroda','Surat','Ahmedabad','Surat','Baroda','Ahmedabad'],
'billamt':[9500,5300,4550,4000,8500,4300,3000],
'tran_date':['2010-10-01','2010-01-04','2009-03-01','2009-04-01','2008-08-05','2008-08-06','2009-10-10']
customer=pd.DataFrame(d)
#Answer 1
customer['discount']=customer['billamt']*0.10
print(customer)
#Answer 2
customer.loc[1008]=['Rohan','Bharuch',6000,'2011-04-01',600]
print(customer)
#Answer 3
plt.bar(customer['cname'],customer['billamt'],color=['r','g','b','c','m','y','k'])
plt.title("Report",color='Red')
plt.xlabel("Customer Names",color='Blue')
plt.ylabel("Bill Amount",color='Magenta')
plt.show()
MySQL
Q – 2 Create below table “staff” and insert all records.
2010-11- Residential
1001 Sagar PGT 87000
02 Section
2010-04-
1002 Ankit Clerk 24000 Office
01
2009-01-
1003 Dhwani Clerk 22000 Office
05
2009-07-
1004 Jenil PRT 34000 Primary
25
2008-07-
1005 Roshni PGT 73000 Senior
17
2008-04-
1006 Mital TGT 41000 Middle
08
Lab 2009-11-
1007 Gagan 24000 Office
Assistant 23
Solution:
import pandas as pd
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)
#Answer 1
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))
#Answer 2
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))
#Answer 3
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')
MySQL
Q – 2 Consider the table “Charity” and write SQL queries for the tasks that
follow:
Table – Charity
vi) Display itemname, price and qty in the descending order of price
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 .
ii. Add a new row of your choice data.
iii. Transfer these data from dataframe to csv named employees.csv.
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.
Solution:
import pandas as pd
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])
#Answer 1
print("Answer 1")
em=employee.loc[:,['Ename','Post','Salary']]
print(em[em.Salary>60000].to_string(header=False,index=False))
#Answer 2
print("Answer 2")
employee.loc[employee.index[-1]+1]=['Ranveer','Analyst',65000,'2020-01-06']
print(employee.iloc[-1])
#Answer 3
employee.to_csv('employees.csv')
#Answer 4
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()
MySQL
Q – 2 Create the table “employee” and write sql queries for the following:
Ans.: max(dt_join)
———————-
2018-10-12
instr(ename,a)
—————-
2
c) Select power(length(post),2) from employee;
Ans.: power(length(post),2)
———————————
49
25
49
49
64
25
d) Select left(ename,4) from employee;
Ans.: left(ename,4)
anil
aksh
ajay
varu
sidd
raje
Python Program
Q – 1 Write a Python code for following questions:
Solution:
import pandas as pd
d={'medicineid':[5147,5274,4296,4175,4385],
'price':[15,20,45,15,85],
ved_medicines=pd.DataFrame(d)
#Answer 1
print("Answer 1")
print(ved_medicines.loc[:,['medicinename','price']].to_string(header=False,index=False))
#Answer 2
print("Answer 2")
print(ved_medicines.tail(4))
#Answer 3
print(ved_medicines[ved_medicines.price>20])
#Answer 4
plt.bar(ved_medicines['medicinename'],ved_medicines['price'],color=['r','g','b','c','y'])
plt.title("Medicine Report",color='blue')
plt.xlabel("Medicine Names",color='red')
plt.ylabel("Price",color='magenta')
plt.legend(['Price'])
plt.show()
MySQL
medicineID Medicinename Price Manufacturer
Observe the above medicine table and write queries for following:
a) Display the unique manufacturer from medicine
————————————-
4
b) Select instr(price,5) from medicine;
Ans.: instr(price,5)
————————–
2
c) Select round(medicineID/2,-1) from medicine;
Ans.: round(medicineID/2,-1)
————————–
2590
2640
2150
2090
2190
Ans.: mid(medicinename,2,4)
arac
-Col
icks
icks
ofra