0% found this document useful (0 votes)
4 views

PYTHON SQL

The document contains Python code for creating and manipulating dataframes using pandas, as well as generating visualizations with matplotlib. It includes three sections: one for medicines, one for employees, and one for countries, each with specific tasks such as displaying data, filtering records, and saving to CSV files. Additionally, it provides SQL query commands for two tables, Charity and Employee, with various data retrieval and manipulation tasks.

Uploaded by

iamwinning1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PYTHON SQL

The document contains Python code for creating and manipulating dataframes using pandas, as well as generating visualizations with matplotlib. It includes three sections: one for medicines, one for employees, and one for countries, each with specific tasks such as displaying data, filtering records, and saving to CSV files. Additionally, it provides SQL query commands for two tables, Charity and Employee, with various data retrieval and manipulation tasks.

Uploaded by

iamwinning1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

I.

i) Write a Python code for following questions :

M_ID M_Name Price Manufacturer


0 5147 Paracetamol 15 Dwarkesh Pharma
1 5274 D-Cold 20 Apollo Pharmacy
2 4296 Vicks VapoRub 45 Procter & Gamble
3 4175 Vicks Action 500 15 Procter & Gamble
4 4385 Soframycin 85 Sanfoli

a) Create above dataframe “Medicines”


b) Display medicines and its price
c) Display last 4 medicines records of medicines whose price is more than 20
d) Transfer these data from dataframe to csv named Med.csv.
ii) Matplotlib program
a) Draw a bar chart which represent medicine name on x-axis and its price on y-axis.
b) Customize the graph by giving proper titles and bar colors.
c) Show the grid lines and save the chart .
#solution 1
import pandas as pd
import matplotlib.pyplot as plt
d={'medicineid':[5147,5274,4296,4175,4385],'medicinename':['Paracetamol','D-
Cold','Vicks Vaporub','Vicks Action 500','Soframycin'], 'price':[15,20,45,15,85],
'manufacturer':['Dwarkesh Pharma','Apollo Pharmacy','Procter & Gamble','Procter &
Gamble','Sanofi']}
ved_med=pd.DataFrame(d)

print(ved_med)
print(ved_med.loc[:,['medicinename','price']].to_string(header=False,index=False))

print(ved_med.tail(4))
print(ved_med[ved_med.price>20])

f = ved_med.to_csv('MED.csv', index = True)

plt.bar(ved_med['medicinename'],ved_med['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.grid(True)
plt.savefig("output1.png")
plt.show()

II. i) Write a Python code for following questions

EName Post Salary Dt_join


101 Anil Manager 65000 2018-03-02
102 Akshay Clerk 33000 2018-05-01
103 Ajay Manager 75000 2018-09-15
104 Varun Analyst 66000 2018-04-11
105 Siddharth Developer 60000 2018-10-12
106 Rajesh Clerk 35000 2018-06-12
a) Create above dataframe “Employee ”
b) Display Name, Post and Salary for all employees earning more than 60000 .
c) Add a new row of your choice data.
d) Transfer these data from dataframe to csv named Emp.csv.
ii) Matplotlib program
a) Create a multi-line chart depicting the Employee Name on x-axis and
their corresponding Salary on y-axis.
b) Give appropriate Graph title, x-axis title, y-axis title, legends and color etc.
c) Show the grid lines and save the chart .

#solution2

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']}

df=pd.DataFrame(d,index=[101,102,103,104,105,106])
print(df)

em=df.loc[:,['Ename','Post','Salary']]
print(em[em.Salary>60000].to_string(header=False,index=False))

df.loc[df.index[-1]+1]=['Ranveer','Analyst',65000,'2020-01-06']
print(df.iloc[-1])

df.to_csv('employees.csv')
plt.plot(df['Ename'],df['Salary'],color='r')
plt.title("Employee Analysis",color='blue')
plt.xlabel("Employee Names",color='red')
plt.ylabel("Salary",color='magenta')
plt.legend(['Salary'])
plt.grid(True)
plt.savefig("EMP.png")
plt.show()

III. Write a Python code for following questions :


Country Population BirthRate UpdateDate
0 China 1379750000 12.00 2016-08-11
1 India 1330780000 21.76 2016-08-11
2 United States 324882000 13.21 2016-08-11
3 Indonesia 260581000 18-84 2016-01-07
4 Brazil 206918000 18-43 2016-08-11
5 Pakistan 194754000 27.62 2016-08-11
Considering the above dataframe answer the following queries by writing appropriate
command in python pandas.
i) Display complete data for China and India.
ii) Display Country, Population and BirthRate of Brazil and Pakistan.
iii) Create a CSV File from the above data frame.
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 and
color etc.

#solution3

import pandas as pd
import matplotlib.pyplot as plt
d={'country':['China','India','US','Indonasia','Brazil','Pakistan'],'population':[13797500
00,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(country)

print(country[country['country']=='China'].to_string(header=False,index=False))
print(country[country['country']=='India'].to_string(header=False,index=False))

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')
SQL QUERIES
1. Write the SQL query commands based on the following table Charity:
Item_id Itemname Price Qty Pdate
1 Shoes 7500 5 2022/11/30
2 Socks 475 3 2022/08/25
3 Jeans 3500 5 2022/10/19
4 T-Shirts 1400 4 2022/11/30

i) Display the name of week day when socks purchased.


ii) Display remainder after dividing price by qty
iii) Increase the price by 10% .
iv) Display the records of items purchased in the month 11.
v) Display maximum qty purchased from the table.
vi) Display itemname, price and qty in the descending order of price
vii) Display item_id, itemname and position of s in each itemname.

2. Write the SQL query commands based on the following table Employee:
NO NAME DEPARTMENT DOJ SALARY SEX

1 RAJA COMPUTER 1998-05-21 80000 M

2 SANGITA HISTORY 1997-05-21 9000 F

3 RITU SOCIOLOGY 1998-08-29 8000 F

4 KUMAR LINGUISTICS 1996-06-13 10000 M

5 VENKATRAAMAN HISTORY 1999-10-31 8000 M

6 SIDHU COMPUTER 1986-05-21 14000 M

7 AISHWARYA SOCIOLOGY 1998-01-11 12000 F

i) To display the records in decreasing order of salary.


ii) To display department and department wise total salary of employees..
iii) To display the Department and its average salary.
iv) To display department and department wise highest salary of the employee.
v) To display sum of salary where date of joining is more than 21/05/97 .
vi) To display length of name of female employee .
vii) To display sum of minimum and maximum salary of employees
3. Consider the following DEPT and WORKER tables. Write SQL queries for (i) to (v)

(i) To display Wno, Name, Gender from the table WORKER in descending order of Wno.
(ii) To display the Name of all the FEMALE workers from the table WORKER.
(iii) To display the WNo and Name of those workers from the table WORKERwho are born
between ‘1987-01-01’ and ‘1991-12-01’.
(iv) To count and display MALE workers who have joined after ‘1986-01-01’.
(v) To display name, department and city of worker whose WNO is less than 1003.

You might also like