Practical File- Informatics Practices (Class XII)
froma
#1 Create a pandas ser jonary of values and an ndarray.
import pandas as pd
‘import nunpy as np
S=pd.Series(np.array([1,3,4,7,8,8,9]))
print(s)
aueuneo
type: int32
# import the pondas Lib os pd
import pandas as pd
# create 0 dictionary
dictionary = {'A' : 18, 'B' : 28, ‘C's 30}
# create a series
series = pd Series(dictionary)
print(serfes)
8
2
%
type: ind
© semved wth Ot Scanner#2. Given a Series, print all the elements that are above the 75th percentile.
import pandas as pd
inport nunpy as np
s=pd.Series(np.array( 1, 3,4,7,8,8,9]))
print(s)
res=s, quantile(q-0.75)
print()
print(’75th Percentile of the series is
print(res)
print()
print(’The elenents that are above the 75th percentile::")
print(s[sores])
type: int32
‘Tsth Percentile of the series i:
8.8
The elenents that are above the 75th percentile:
6 9
type: int32
© semved wth Ot Scanner#3 Create a Data Frame quarterly sales where each row contains the item
category, item name, and expenditure, Group the rows by the category, and
port pandas as pd
"s['car' "Ae "Aircoler’, ‘Washing Machine’ J,
"Ford' ,'Hitachi', ‘Symphony’ ,"LG'],
‘expenditure’ ;[7808008, 58000, 12000, 14@]}
quartsales-pd.DataFrane(dic)
print (quartsales)
qs-quartsales. groupby(itencat’)
print('Result after Filtering Datafrane')
print(qs[ 'itencat', ‘expenditure’ ].sun())
itemcat itemname expenditure
@ car Ford 7090082
1 Ac Hitachi 5000e
2 Aircoller Symphony 1200
3 Washing Machine 6 14000
Result after Filtering Datafrane
expenditure
itencat
Ac 50000
Aircoller 12006
Washing Machine 14000
car 7202
BTr ane
© semved wth Ot Scanner#4. Create a data frame based on ecommerce data and generate desc
{mean, median, mode, quartile, and variance).
import pandas as pd
sales = {'InvoiceNo': [1021,1002,1203, 1004, 1085, 1006, 1807],
“ProductNane’: ['LED',"AC',‘Deodrant’, "Jeans', Books’, Shoes", Jacket’),
“Quantity’s (2,1,2,1,2,1,1],,
“Brice: (65808, $5808, 580,258,950, 3008, 2208)
df=pdDataFrane(sales)
print(Gf[ Price]. describe() round(2))]
count 7.80
mean 18450,00
std 2843.61
min 520.00
ah 1575.00
50h 2508.00
75% = 29008.00
max 65000.00
lane: Price, dtype: floats
© semved wth Ot Scanner#5. Create a data frame for examination result and
labels data types of each column and the dime!
‘inport pandas as pd
dic={*Class's['I', IL’ "III", "IV','V','VI','VIN", ‘VEIN, "IX','X', "XI", XII],
“pass-Percentage’:[120, 108, 102, 108, 180, 108, 108, 180, 120,98.6,100,99)}
result=pd.DataFrame(dic)
print(result)
print(result.dtypes)
print('shape of the dataframe is
print (result. shape)
Class Pass-Percentage
e I 100.0
160 100.8
2 Wl 106.0
3 100.8
4 v 106.8
5 WwW 120.8
6 VII 100.8
7 MII 100.0
8 IK 100.8
9 Xx 98.6
eX 100.0
1 XI 99.8
Class object
Pass-Percentage float
type: object
shape of the datafrane 4
(12, 2)
splay row label:
column
© semved wth Ot ScannerIter out rows based on different criteria such as duplicate rows.
import pandas as pd
dice{'Nane' :['Rohit',Hohit', Deepak’, ‘Rohit’, Deepak’, 'Sohit', ‘Geeta’ ],
"MarksintP :[85,45,92,85,92,96,84]}
arks=pd. DataFrane(dic)
# Find duplicate rows
duplicateRow = narks{marks duplicated (keep-False)
print (duplicateRow)
Name NarksinIP
@ Rohit 8
2 Deepak 2
3 Rohit 5
4 Deepak 2
BTPage
0 sened wth 1 San‘ind the sum of each column, or find the column with the lowest mean.
‘import pandas as pd
Profit={ ‘TCS’: { ‘Qtri':2500, ‘Qtr2':2800, ‘Qtr3":3688, 'Qtr4' :2000},,
"WIPRO": {*Qtra" :2800, Qtr2' :2400, ‘Qtr3”:3608, “Qtr” :2406},
"war's { ‘ote
.Datafrane(Profit)
print df)
print()
print (‘Colum wise sun in datfrane is
print df. sum(axis-0))
# Print mean value of each column
print()
print(‘Colum wise mean value are::
print (mean (axis-6))
# Returns Column with minimum meon value
print()
print (‘Colum with mininun mean value is:
df mean(axis-8) idxnin()
100, ‘Qtr2' :5708, ‘Qtr3' :35888, ‘Otr4' :2108}}
TCS WIPRO L&T
Qtr 25¢e 2888 2108
Qtr2 208@ 2400 5708
Qtr3 38¢0 3608 35000
Qtr 2000 2400 © 2100
Colunn wise sum in datfrane is
1s 9500
WIPRO 11200
lat 44900
dtype: intea
Column wise mean value a
1s 2375.0
WIPRO 2800.0
aT 1122.0
type: floatea
Column with minimun mean value is:
"Tes"
Thane
© semved wth Ot Scannerocate the 3 largest values in a data fram
‘import pandas as pd
dic={'Name' :['Rohit’ ‘Mohit’ , ‘Deepak’ ‘Anil’, ‘Pankaj’, 'Sohit’, Geeta’),
"MarksinIP" :[85,45,92,85,98,96,84]}
mnarks=pd.DataFrane(dic)
# Find 3 Largest Value for MarksinIP Column
print(marks nlargest(3,['MarksinIP']))
Name MarksinIP
4 Pankaj 98
5 Sohit 96
2 Deepak 2
BTPage
0 sened wth 1 San#9. Subtract the mean of a row from each element of the row in a Data Fram
Anort pandas as pd
Profite{ ‘TCS': { ‘Qtrt':2580,"(Qtr2':20e@, Qtr3':3808, ‘tra: 2888},
"WIPRO': {"Qtrt' 2808, 'Qtr2':2488, ‘Qtr3': 3688, Qtr :24@¢),
“UT's { "Qtrt': 2168, "Qtr
f-pd.Datatrane(Profit)
print(¢f)
print()
print (‘ean of each rou i
print (df.nean(axiss1))
prit()
print('Datafrane after Subtracting nean value of each row fron each elenent of that Row is. :::')
print (df. sub(.nean(axis=t), axds-2))
35700, 'Qtr3" :35808, “Qtr4' 2180}
TCS MIRO LET
Qtr 2508 2888 2106
Qtr 2808 2408 5700
Qtr? 3000 3608 35000
Qtr 2608 2488 2100
‘Mean of each row i
Qtri 2466. 666667
Qtr? 3366. 666667
Qtr3— 13866.666667
tre 2166.666667
dtyper floats
Datafrane after Subtracting mean value of each row from each element of that Row is
us WIPRO ut
Qtr 33,333333 333.3333 -366.666667
Qtr2 -1366.666667 -966,666667 2333.333333
Qtr} -10866.666667 -10266.666667 21133.333333
Qtr -166,666667 233.333333 -66.666667
3TPage
© semved wth Ot Scanner#10. Replace all negative values in a data frame with a
‘import pandas as pd
dice{"Datal! :[-5,-2,5,8,9,-6],
"Datad' :[2,4,10,15,-5,-8]}
dfepd.DataFrane(dic)
print(df)
print()
print(“dataFrane after replacing negative values with @:::")
FL aco] <0
print( of)
Oatal Dated
qo 2
1 2 4
2 5 18
3 §
409 5
5 6 8
dataFrane after replacing negative values with @:::
Datal Data?
e @ 2
3 e 4
2 5
3 8
4 9 e
5 e e
TOTS
© semved wth Ot Scanner#11. Replace all missing values in a data frame with a 999.
‘inport pandas as pd
‘import nunpy as np
enpdata={ ‘enpid':[101,1€2,103, 104,105,106),
“eae: ['Sachin', Vinod’, Lak’ p.nan,'Devinde', 'Unaselvi'),
"Do! s['12-81-2012', "15-01-2012" "85-09-2007", '17-21- 2012" np.nan, '16-€1-2012' ]}
df-pd.Datafrane(enpdata)
print (df)
df-df. Fillna({"enane' :999, '
print()
print (df)
:299))
empid ene Oj
101 Sachin 12-01-2012
102 Vinod 15-@1-2012
103 Lakhbir 05-09-2007
1 hall 17-41-2012
105 Devinder Na
106 UnaSelvi 16-01-2012
emit enane Oj
el Sachin 12-01-2012
18 Vinod 15-01-2012
103 Lakhbir 05-09-2067
1e4 999 17-01- 2012
405, evinder eg
105 UnaSelvi 16-€1-2012
TiTPage
© semved wth Ot Scanner#12. Importing and exporting data between pandas and CSV file
# importing pandas module
2 import pandas as pd
# making data frane
4 df = pd.read_csv("E:\enp.csv")
5 print(d)
enpid enane oj
@ 181 Sachin Bhardwaj 12-€1-2012
1 102 Vinod Verma 15-@1-2012
2 183 Anand Ganesh 05-09-2007
1 import pandas as pd
2 1 [{'Name'; ‘Sachin’, 'Sirfane':'Bhardwaj'},
3 {'Name': "Vinod’, "SirNane’:'Verma"},
4 { ‘Name's ‘Rajesh’ , 'Sirtiane’ s ‘Mishra’ }]
5 dfl=pd.DataFrane(1)
6 # saving the datafrane
7 dfl.to_csv("E:\Datafranel .csv')
co twvrvYrYtectc= Fw
ER con oe) ae) Pmere soe Bh ee:
OAs EES RE aes Fo 4 EA ote ee Ge
z Agomert meer ye:
rr odd
1 Name SirName
2 O Sachin Bhardwaj
3 1 Vinod Verma
+2 Rajesh Mishra
5
TTP are
© semned ith nt Seamer#13. Importing and exporting data between pandas and MySQL databas.
Importing Data from MySQL to Data Frame.
In [1]: imort pandas as pd
In (2): inportaysl.comector
In (5]: can-nysql.comector connect (host="Iocalhast’,user
In [6]: enppdeadsq.qry( "show ta
a
fron sachin" con)
Tables in, secin
0 enplyee
© semved wth Ot ScannerExporting data from Data Frame to MYSQL.
[8]: import nysql.cmector
ingot pandas as pd
conaysql connector connect (tasts"loa
print(con)
cursor)
printf)
c.erecate
con,comit()
for(royrs) in df. iteros()
capes int(0]))
*s{!]
s2])
databases
fe from employee")
concn
)
{ “enpid”:[101, 102, 103,1
enane': [Sachin fi
(212-120-0115 207-0-5' "1-0-1727, "2012-41-16 }
fp. tataFrane gata)
print( "Ota transfer Successfully")
105,106],
select * from student;
| studentid | name I
en -+-=-
| 2 | SACHIN |
I 2 | amrr ot
+---- -+-5 a+
2 rows in set (€0.01 sec)
udentbirth;
I
| 2 | 1982-06-17 |
I 2 | 1981-05-20 |
2 rows in set (€0.00 sec)
mysql> create table stu2 as select name,dob from student, studentbirth
-> where student .studentid=studentbirth.studentid;
Query OK, 2 rows affected (0.37 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from stu2;
++ as
| name | dob i
+ a+
| SACHIN | 1982-06-17 |
| ArT | 1981-05-20 |
$+ a+
2 rows in set (0.00 sec)
#26. Write a SQL query to order the (student ID, marks) table in descending order
of the marks.
Buen
0 seamed vith one Seamer