Journal File Akshada R Bafna
1. Write a program to create a series object with 6 random integers
and having indexes as: [‘p’,’q’,’r’,’n’, ’t’,’v’].
Code:
import pandas as pd
import numpy as np
s=pd.Series(np.random.randint(6,size=6),index=['p','q
','r','n','t','v',])
print(s)
output:
065-Informatic practices pg. 1
Journal File Akshada R Bafna
2. Write a program to create a data series and then change the indexes
of the series object in any random order.
Code:
import pandas as pd
import numpy as np
s1=pd.Series(data=[100,200,300,400,500],
index=['I','J','K','L','M'])
print("original data series:")
print(s1)
s1=s1.reindex(index=['K','I','M','L','J'])
print("data series after changing the order of index:
")
print(s1)
output:
065-Informatic practices pg. 2
Journal File Akshada R Bafna
Q3. Write a program to sort the values of series object s1 in
descending order of its indexes and store it into series object s3.
S1 = [6700,5600,5000,5200].
Code:
import pandas as pd
s1=pd.Series(data=[6700,5600,5000,5200],index=[1,2,3,
4])
s3=s1.sort_index(ascending=False)
print("Series object s1:")
print(s1)
print("Series object s3:")
print(s3)
Output:
Series object s1:
1 6700
2 5600
3 5000
4 5200
dtype: int64
Series object s3:
4 5200
3 5000
2 5600
1 6700
dtype: int64
065-Informatic practices pg. 3
Journal File Akshada R Bafna
Q4. Write a program to create a 3-size series using input () and
calculate the cube of the series value.
Code:
import pandas as pd
n=eval(input("Enter any 3 numbers of your choice:"))
s4=pd.Series(n)
print("Series object s4:")
print(s4)
print("Cubes of s4 values:")
print(s4**3)
Output:
Enter any 3 numbers of your choice:2,5,6
Series object s4:
0 2
1 5
2 6
dtype: int64
Cubes of s4 values:
0 8
1 125
2 216
dtype: int64
065-Informatic practices pg. 4
Journal File Akshada R Bafna
Q5. Write a program to create series object that stores the table of
number 7.
Code:
import pandas as pd
import numpy as np
n=np.arange(1,11)
s5=pd.Series(n*7)
print("Series displaying table of 7:")
print(s5)
Output:
Series displaying table of 7:
0 7
1 14
2 21
3 28
4 35
5 42
6 49
7 56
8 63
9 70
dtype: int64
065-Informatic practices pg. 5
Journal File Akshada R Bafna
Q6. Write a program to create a DataFrame storing salesman details
(name, zone, sales) of 5 salesmen.
Code:
import pandas as pd
n={'Name':['Raj','Kunal','Sam','Joy','Shiv'],
'Sales':[1000,2000,4000,8000,16000]}
i=['Zone1','Zone2','Zone3','Zone4','Zone5']
df1=pd.DataFrame(data=n,index=i)
print("DataFrame containing sales man details:")
print(df1)
Output:
DataFrame containing sales man details:
Name Sales
Zone1 Raj 1000
Zone2 Kunal 2000
Zone3 Sam 4000
Zone4 Joy 8000
Zone5 Shiv 16000
065-Informatic practices pg. 6
Journal File Akshada R Bafna
Q7. Create the following DataFrame Sales containing yearwise sales
figures for five sales persons in INR. Use the years as column labels,
and salesperson names as row label.
Code:
import pandas as pd
SaleDict={2014:{'Madhu':100.5,'Kusum':150.8,'Kinshuk'
:200.9,'Ankit':30000,'Shriti':40000},
2015:{'Madhu':12000,'Kusum':18000,'Kinshuk':22000,'An
kit':30000,'Shriti':45000},
2016:{'Madhu':20000,'Kusum':50000,'Kinshuk':70000,'An
kit':100000,'Shriti':125000},
2017:{'Madhu':50000,'Kusum':60000,'Kinshuk':70000,'An
kit':80000,'Shriti':90000}}
sales=pd.DataFrame(SaleDict)
print("DataFrame containing Year-wise sales:")
print(sales)
Output:
DataFrame containing Year-wise sales:
2014 2015 2016 2017
Madhu 100.5 12000 20000 50000
Kusum 150.8 18000 50000 60000
Kinshuk 200.9 22000 70000 70000
Ankit 30000.0 30000 10000 80000
Shriti 40000.0 45000 125000 90000
065-Informatic practices pg. 7
Journal File Akshada R Bafna
Q8. Use the DataFrame created in Question 7 above to do the following:
a) Display the row labels of Sales.
b) Display the column labels of Sales.
c) Display the data types of each column of Sales.
d) Display the dimensions, shape, size and values of Sales.
e) Display the last two rows of Sales.
f) Display the first two columns of Sales.
Code:
a) sales.index
b) sales.columns
c) sales.dtypes
d) sales.ndim,sales.shape,sales.size,sales.values
e) sales.iloc[3: , ]
f) sales.iloc[ : , :2]
Output:
a) Index(['Madhu', 'Kusum', 'Kinshuk', 'Ankit',
'Shriti'], dtype='object')
b) Index([2014, 2015, 2016, 2017], dtype='int64')
c)
2014 float64
2015 int64
2016 int64
2017 int64
dtype: object
065-Informatic practices pg. 8
Journal File Akshada R Bafna
d)
(2 (5, 4) 20 [[1.005e+02 1.200e+04 2.000e+04 5.000e+04]
[1.508e+02 1.800e+04 5.000e+04 6.000e+04]
[2.009e+02 2.200e+04 7.000e+04 7.000e+04]
[3.000e+04 3.000e+04 1.000e+05 8.000e+04]
[4.000e+04 4.500e+04 1.250e+05
9.000e+04]])
e)
2014 2015 2016 2017
Ankit 30000.0 30000 100000 80000
Shriti 40000.0 45000 125000 90000
f)
2014 2015
Madhu 100.5 12000
Kusum 150.8 18000
Kinshuk 200.9 22000
Ankit 30000.0 30000
Shriti 40000.0 45000
065-Informatic practices pg. 9
Journal File Akshada R Bafna
Q9. Create a dictionary using the following data. Use this dictionary to
create a DataFrame Sales2.
2018
Madhu 160000
Kusum 110000
Kinshuk 500000
Ankit 340000
Shruti 900000
Check if Sales2 is empty or it contains data.
Code:
import pandas as pd
d1={2018:[160000,110000,500000,340000,900000]}
sales2=pd.DataFrame(d1,index=['Madhu','Kusum','Kinshu
k','Ankit','Shruti'])
print(sales2)
print(sales2.empty)
Output:
2018
Madhu 160000
Kusum 110000
Kinshuk 500000
Ankit 340000
Shruti 900000
False
065-Informatic practices pg. 10
Journal File Akshada R Bafna
Q10. Use the DataFrame created in Question 9 above to do the
following:
a) Append the DataFrame Sales2 to the DataFrame Sales.
b) Change the DataFrame Sales such that it becomes its transpose.
c) Display the sales made by all sales persons in the year 2017.
Code:
a) sales=pd.append(sales2)
print(sales)
b) sales=sales.T
print(sales)
c) sales.loc[ : ,2017] # with original dataframe
Tsales.loc[2017, : ] #with Transposed dataframe
Output:
a)
2014 2015 2016 2017 2018
Madhu 100.5 12000 20000 50000 160000
Kusum 150.8 18000 50000 60000 110000
Kinshuk 200.9 22000 70000 70000 500000
Ankit 30000.0 30000 100000 80000 340000
Shriti 40000.0 45000 125000 90000 900000
b)
Madhu Kusum Kinshuk Ankit Shruti
2014 100.5 150.8 200.9 30000.0 40000.0
2015 12000.0 18000.0 22000.0 30000.0 45000.0
2016 20000.0 50000.0 70000.0 100000.0 125000.0
2017 50000.0 60000.0 70000.0 80000.0 90000.0
2018 160000.0 110000.0 500000.0 340000.0 900000.0
065-Informatic practices pg. 11
Journal File Akshada R Bafna
c)
Madhu 50000 #with original
dataframe
Kusum 60000
Kinshuk 70000
Ankit 80000
Shriti 90000
Name: 2017, dtype: int64
Madhu 50000.0
Kusum 60000.0
Kinshuk 70000.0
Ankit 80000.0
Shriti 90000.0
Name: 2017, dtype: float64
065-Informatic practices pg. 12
Journal File Akshada R Bafna
Q11. Use the DataFrame created in Question 9 above to do the
following:
a) Display the sales made by Madhu and Ankit in the years 2017
and 2018.
b) Display the sales made by Shruti 2016.
c) Add data to Sales for salesman Sumeet where the sales made are
[196.2, 37800, 52000, 78438, 38852] in the years [2014, 2015,
2016, 2017, 2018] respectively.
Code:
a) sales.loc[['Madhu','Ankit'],[2017,2018]]
Tsales.loc[[2017,2018],['Madhu','Ankit']]
b) sales.loc[['Shruti'],[2016]]
Tsales.loc[[2016],['Shruti']]
c) sales.loc['Sumeet',:]=[196.2,37800,52000,78438,38
852]
print(sales)
Output:
a)
2017 2018
Madhu 50000.0 160000.0 #with original dataframe
Ankit 80000.0 340000.0
Madhu Ankit
2017 50000.0 80000.0 #with transposed dataframe
2018 160000.0 340000.0
b)
2016
Shruti 125000
Shruti
2016 125000.0
065-Informatic practices pg. 13
Journal File Akshada R Bafna
c)
2014 2015 2016 2017 2018
Madhu 100.5 12000 20000 50000 160000
Kusum 150.8 18000 50000 60000 110000
Kinshuk 200.9 22000 70000 70000 500000
Ankit 30000.0 30000 100000 80000 340000
Shriti 40000.0 45000 125000 90000 900000
Sumeet 196.2 37800 52000 78438 38852
065-Informatic practices pg. 14
Journal File Akshada R Bafna
Q12. Use the DataFrame created in Question 9 above to do the
following:
a) Delete the data for sales man Kinshuk from the DataFrame Sales.
b) Change the name of the salesperson Ankit to Vivaan and Madhu to
Shailesh.
c) Update the sale made by Shailesh in 2018 to 100000.
Code:
a) sales.drop(['Kinshuk'])
b) sales=sales.rename(index={'Ankit':'Vivaan','Madhu
':'Shailesh'})
print(sales)
c) sales.loc['Shailesh',2018]=100000
Output:
a)
2014 2015 2016 2017 2018
Madhu 100.5 12000 20000 50000 160000
Kusum 150.8 18000 50000 60000 110000
Ankit 30000.0 30000 100000 80000 340000
Shriti 40000.0 45000 125000 90000 900000
Sumeet 196.2 37800 52000 78438 38852
b)
2014 2015 2016 2017 2018
Shailesh 100.5 12000 20000 50000 160000
Kusum 150.8 18000 50000 60000 110000
Kinshuk 200.9 22000 70000 70000 500000
Vivaan 30000.0 30000 100000 80000 340000
Shriti 40000.0 45000 125000 90000 900000
Sumeet 196.2 37800 52000 78438 38852
065-Informatic practices pg. 15
Journal File Akshada R Bafna
c)
2014 2015 2016 2017 2018
Shailesh 100.5 12000 20000 50000 100000
Kusum 150.8 18000 50000 60000 110000
Kinshuk 200.9 22000 70000 70000 500000
Vivaan 30000.0 30000 100000 80000 340000
Shriti 40000.0 45000 125000 90000 900000
Sumeet 196.2 37800 52000 78438 38852
065-Informatic practices pg. 16
Journal File Akshada R Bafna
Q13. Write a program to read details such as Items, Sales in a
DataFrame and then store this data in a CSV file.
Code:
import pandas as pd
import csv
df=pd.DataFrame({'Item':['Books','Pens','Erasers'],
'sales':[100,200,300]})
print(df)
df.to_csv("d:\\Users\\ABC\\Desktop\\IP\\sales.csv")
print("sucessfully stored")
Output:
Item sales
0 Books 100
1 Pens 200
2 Erasers 300
sucessfully stored
065-Informatic practices pg. 17
Journal File Akshada R Bafna
Q14. Write a program that read from CSV file where the separator
character is ‘$’. Read only first 5 rows in your DataFrame.
• Give column headings ItemName, Quantity, price.
• Make sure to read first row as data not as column headers.
Code:
import pandas as pd
df=pd.read_csv("d:\\Users\\ABC\\Desktop\\IP\\data.csv
",
sep='$',names=['Itemname','Quantity','Price'],header=
None,nrows=5)
print(df)
Output:
Itemname Quantity Price
0 Soaps
1 Shampoo
2 Oil bottles
3 Comb
4 Detergent
065-Informatic practices pg. 18
Journal File Akshada R Bafna
Q15. Write a program that shows a successful connection between
Python and MySQL.
Code:
import pandas as pd
import mysql.connector as sqltor
mycon=sqltor.connect(host=”localhost”,user=”root”,
passwd=1234,database=”nupur12s”)
if mycon.is_connected( ):
print(“Successfully connected to MySQL
database”)
Output:
065-Informatic practices pg. 19
Journal File Akshada R Bafna
Q16. Write a program to draw line chart from given financial data of
ABC Co. for 5 days in form a DataFrame namely fdf as shown:
Day 1 Day 2 Day 3 Day 4 Day 5
0 74.25 56.03 59.30 69.00 89.65
1 76.06 68.71 72.07 78.47 79.65
2 69.50 62.89 77.65 65.53 80.75
3 72.55 56.42 66.46 76.85 85.08
Code:
import pandas as pd
import matplotlib.pyplot as plt
a={'day1':[74.25,76.6,69.50,72.55],
'day2':[56.03,68.71,62.89,56.42],
'day3':[59.30,72.07,77.65,66.46],
'day4':[69.00,78.47,65.53,76.85],
'day5':[89.65,79.65,80.75,85.08]}
i=[0,1,2,3]
fdf=pd.DataFrame(a,i)
print(fdf)
fdf.plot()
plt.show()
065-Informatic practices pg. 20
Journal File Akshada R Bafna
Output:
065-Informatic practices pg. 21
Journal File Akshada R Bafna
Q17. Write a program to create a horizontal bar chart for India’s
medal tally.
Code:
import matplotlib.pyplot as plt
Info=['Gold','Silver','Bronze','Total']
India=[26,20,20,66]
plt.ylabel("Medal Type")
plt.xlabel("Medal Count")
plt.title("India's Medal Tally in Commonwealth 2018")
x=range(len(Info))
plt.barh(x,India,color=['gold','silver','brown','blac
k'])
plt.show()
065-Informatic practices pg. 22
Journal File Akshada R Bafna
Output:
065-Informatic practices pg. 23
Journal File Akshada R Bafna
Q18. Prof Raj is doing some research in the field of environment. For
some plotting purpose, he has generated some data are:
mu=100
sigma=15
x=mu+sigma*numpy.random.randn(10000)
y=mu+30*np.random.randn(10000)
Write a program to plot this data on a cumulative barstacked
horizontal with both x and y-axes.
Code:
import numpy as np
import matplotlib.pyplot as plt
mu=100
sigma=15
x=mu+sigma*np.random.randn(10000)
y=mu+30*np.random.randn(10000)
plt.hist([x,y],bins=100,histtype='barstacked',cumulat
ive=True)
plt.title('research data histogram')
plt.show()
065-Informatic practices pg. 24
Journal File Akshada R Bafna
Output:
065-Informatic practices pg. 25
Journal File Akshada R Bafna
19. Write a program to plot a bar chart from the medals won by top
four countries. Make sure that bars are separately visible.
Code:
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(10,7))
info=['gold','silver','bronze','total']
australia=[80,59,59,198]
england=[45,45,46,136]
india=[26,20,20,66]
canada=[15,40,27,82]
x=np.arange(len(info))
plt.bar(info,australia,width=0.15)
plt.bar(x+0.15,england,width=0.15)
plt.bar(x+0.30,india,width=0.15)
plt.bar(x+0.45,canada,width=0.15)
plt.show()
065-Informatic practices pg. 26
Journal File Akshada R Bafna
Output:
065-Informatic practices pg. 27
Journal File Akshada R Bafna
Consider the following table named “Product”, showing details of
products being sold in a grocery shop.
065-Informatic practices pg. 28
Journal File Akshada R Bafna
20.Create the table Product with appropriate data types and
constraints. Insert all records as mentioned.
Code:
065-Informatic practices pg. 29
Journal File Akshada R Bafna
Output:
065-Informatic practices pg. 30
Journal File Akshada R Bafna
21.Add a new column Discount to the table Product
Code:
Output:
065-Informatic practices pg. 31
Journal File Akshada R Bafna
22.Increase the price by Rs.12 for all the products manufactured by
Dove.
Code:
Output:
065-Informatic practices pg. 32
Journal File Akshada R Bafna
23.Display the total number of products manufactured by each
manufacturer
Code:
Output:
065-Informatic practices pg. 33
Journal File Akshada R Bafna
Write the output produced by the following SQL commands:
24.SELECT POW(2,3);
Code:
Output:
065-Informatic practices pg. 34
Journal File Akshada R Bafna
25.SELECT ROUND(123.2345, 2), ROUND(342.9234,-1);
Code:
Output:
065-Informatic practices pg. 35
Journal File Akshada R Bafna
26.SELECT YEAR(“1979/11/26”), MONTH(“1979/11/26”),
DAY(“1979/11/26”), MONTHNAME(“1979/11/26”);
Code:
Output:
065-Informatic practices pg. 36
Journal File Akshada R Bafna
27.SELECT LEFT("INDIA",3), MID("Informatics",3,4),
RIGHT("Computer Science",4);
Code:
Output:
065-Informatic practices pg. 37
Journal File Akshada R Bafna
Consider the following tables Student and Stream in the
Streams_of_Students database. The primary key of the Stream table is
StCode (stream code) which is the foreign key in the Student table.
The primary key of the Student table is AdmNo (admission number).
065-Informatic practices pg. 38
Journal File Akshada R Bafna
28.Jay has now changed his stream to Humanities. Write an
appropriate SQL query to reflect this change.
Code:
Output:
065-Informatic practices pg. 39
Journal File Akshada R Bafna
29.Display the names of students whose names end with the character
‘a’. Also, arrange the students in alphabetical order.
Code:
Output:
065-Informatic practices pg. 40
Journal File Akshada R Bafna
30.List the number of students in each stream having more than 1
student.
Code:
Output:
065-Informatic practices pg. 41
Journal File Akshada R Bafna
31.Display the names of students enrolled in different streams, where
students are arranged in descending order of admission number
Code:
Output:
065-Informatic practices pg. 42
Journal File Akshada R Bafna
Write the output(s) produced by executing the following queries on
the basis of the information given above in the table Product:
065-Informatic practices pg. 43
Journal File Akshada R Bafna
32.SELECT PName, Average(UPrice) FROM Product GROUP BY Pname;
Code:
Output:
065-Informatic practices pg. 44
Journal File Akshada R Bafna
33.SELECT DISTINCT Manufacturer FROM Product;
Code:
Output:
065-Informatic practices pg. 45
Journal File Akshada R Bafna
34.SELECT COUNT(DISTINCT PName) FROM Product;
Code:
Output:
065-Informatic practices pg. 46
Journal File Akshada R Bafna
35.SELECT PName, MAX(UPrice), MIN(UPrice) FROM Product GROUP
BY PName;
Code:
Output:
065-Informatic practices pg. 47
Journal File Akshada R Bafna
065-Informatic practices pg. 48
Journal File Akshada R Bafna
065-Informatic practices pg. 49
Journal File Akshada R Bafna
065-Informatic practices pg. 50