0% found this document useful (0 votes)
85 views23 pages

DocScanner 09-Dec-2024 6-45 PM

Ip class 12 practical

Uploaded by

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

DocScanner 09-Dec-2024 6-45 PM

Ip class 12 practical

Uploaded by

Vikramjeet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 23
PRATICAL 1 Problem statement: 1. Create a Series object using the python sequence with 5 elements: Solution: Source Code: import pandas as pd L = [12,23,34,45,56] S= pd.Series(S) print (Ss) Screenshot: LG "Series Prel.py - CAUsersiuse/MAppDsta\LocahPrograms\Python\Pythond7-22\Seies-Pripy 76" = [= JS |e) File Edit {Format| Run Options Window Help import pandas as pd L = [12,23,34,45,56] S= pd. Series(s) print (s) Output =) RESTART :) C: \Users\user\appData ais 23 Bhat PRATICAL 2 Problem statement: 1. Create a Series object ‘vowel’ to store all vowels individually. Its index should be 1,2,3,4 & 5.: Solution: Source Code: import pandas as pd vowels = pd.Series(['a','e','i','o','u'], index = [1,2,3,4,5]) print (vowels) Screenshot: “B Senener-2 py CAUSEe\dmiMApEUAEALacanProgramAPytnonPyharsISererPrep. OX File Edit Format Run Options Windaw Help. Pera ease “ vowels = pd-Series(['a!,'e!,'i','o!,'u'], index = [1,2,3,4,5]} parse RCe ASE) Output PSU Eee Er ae re) a u Des EES aio 1 FY 3 4 at PRATICAL 3 Problem statement: Create s Series object using ndarray that has 5 elements in the range 50 and 100: Solution: Source Code: import pandas as pd import numpy as np 8 = pd. Series (np. arange (50,100,10)) print (Ss) Screenshot: TA, Series-Pr-J.ay - CAUserstadmin\AppDatakLocal\Programs\Python\Python31\Series-Pr-d.py (3.102) File Edit Format Run Options Window Help eae import numpy as np pe eet ret ee et ACTOR Be KB) sae Output RESTART: /C:\Users\admin\Ap| 50 60 70 80 90 CUS eae ed PRATICAL 4 Problem statement: Create a Series object using dictionary to that stores the no of students in each section of class 12th of your school: Solution: Source Code: import pandas as pd D = {1A':23,'B':34,'C':36,'D':40,"E' :32} S = pd.Series(D) print (S) Screenshot: sevies-Prt py CAUsersintmninvppDat\Locahrograme\eytnon\Pythan SIM Seres-Prpy G10) File Edit Format Run Options Window Help eae en ee D = ('A':23)'B!:34)'¢':36,'D':40,'E':32} eg bese Kt) See T eS) Output ere Senet nC Ce rene Fy F) ¢ i) z ct | PRATICAL 5 Problem statement: Total no of students to be admitted is 350 in Yojna School every year. Write code to create a Series object ‘School’ that stores these total no of students for the year 2015 to 2022: Solution: Source Code: import pandas as pd 8 = pd.Series(350, range(2016,2023)) print (Ss) Screenshot: TP Series-Pr-5 py - CAUsers\admin\AppDete\LocahPrograms\Python'Python310\Series-Pr-S.py (197) File Edit Format Run Options Window Help pee rae CC ee at yee Be tse CL Sear pe print (s) Se UPR e eRe eke Pies) 350 PCr 2018 350 Pr EET) PEE Pes ET) ith 350 Cea eee E CLT} PRATICAL 6 Problem statement: Create a Series object ‘Item’ that stores rate of each product as given below: Soap 54 Salt 20 Sugar 39 Write code to modify rate of soap to 44 and sugar to 42. print the changed rate: Solution: Source Code: import pandas as pd 8 = pd.Series ([54,20,39], ['soap', ‘salt’, ‘sugar']) print (Ss) S['soap’ S['sugar’ print ("After Updating values") print (Ss) 4 Screenshot: TA, Series-Pr-S py - C:\Users\adrmin\AppQats\Locsl\Programs\Pythan\Python31O\Series-Pr-6 py (3.10.2) File fait Format Run Options Windew Help se re) eee COOe ree ae ene kt ae) print (5) eer areal pests te] Output aCe eC ae ue Ine soap Ey Ev 39 Bett) et aaa PRATICAL 7 Problem statement: No of students in class 11 and class 12 in three streams (science, commerce and humanities) are stored in 2 series object class 11 and class 12. write code to find total no of students in class 11 & class 12 stream wise: Solution: Source Code: import pandas as pd Di= {'Science' :32,'Commerce D2= {'Science':28,'Commerce Class11 = pd. Series (D1) Class12 = pd. Series (D2) print (Class11) print (Class12) print (‘Total Students') print (Class11 + Class12) 36,'Humanities':20} 34,"Humanities’:22} Screenshot: TF Seen Pe-Tpy- CAUsem\adini\paOateLacehProsranm\ Python Pyenen3iONSries-Pr7py 107) Fle dit Formet_ fun Options Window Help er eee) i See Wate Ee ah Terre D2= ('Science':28, ‘Commerce’: 34, 'Humanities' :22) Porer bee ee ace thse) Class12 = pd.Series (D2) print (Classi1) poeta eh eres) Post ntertie tr eos) print (Classi1 + Class12) rs\adnin\| Ermerirerss) preety Poercrirtcy Poteet ee corey Peeters) Poetry PRATICAL 8 Problem statement: Create a Series object ‘population’ to store population of 5 different metro cities and display the population that are more than 300000: Solution: Source Code: import pandas as pd population = pd.Series([400000, 25400, 301100, 100500,505000], ['Mumbai', 'Kolkata’, 'Delhi print (population) print('Poplulation more than 300000') print (population [population>300000]) Screenshot: TR Series Pr-Spy- C\Users\admin\AppDatall aca Programe\Python\ Pythan 31 Series Pr-2.py (2.107) = ¢ File _Esit_ Format Deh Ones population = pd-Series([400000, 25400, 301100, 100500,505000], err ccre cee Cec DS Frits CERO -EL pert ron) ,'Chennai', 'Bangluru']) Fun Options Windew Help. print (population) Poste Sore Coe MCU LD) Peretti e eat hese ect TL t pp) Output Cee CURR EET eae ree ss Mumbai ECT) cob chy 25400 Pests 301100 Peru POLE) Bangluru 505000 Cia eer) Poplulation more than 300000 aris ee 400000 PCRt Se lene els) Bangluru Eteh-telels) pee eer) PRATICAL 9 Problem statement: Create a series ‘temp’ that stores temperature of seven days in it. Its index should be ‘Sunday’, ‘Monday’ .... Write script to 1. Display temp of first 3 days. 2. Display temp of last 3 days. 3. Display all temp in reverse order like Saturday, Friday,.... 4. Display temp from Tuesday to Friday. 5. Display square of all temperature.: Solution: Source Code: import pandas as pd temp = pd.Series([45,42,40,46,39,38,40], ['Sunday', 'Monday’, ‘Tuesday’, 'wednesday', 'Thursday', 'Friday', 'Saturday'] ) print (temp) print ("Temp of first three days\n", temp.head(3)) print ("Temp of last three days\n", temp. tail(3)) print ("Temp in reverse order\n", temp[::~-1]) print ("Temp from Tuesday to Friday\n",temp['Tuesday' : 'Friday']) print ("Square of all Temprature\n", temp*temp) Screenshot: Sees Pray Ciesla rogers higher SNe Pp 7) Print (temp) patent ear) pore ee eae, ee pats eer Se eC oen L rer ret Cait) pets ees ey Pe rr eee Cee Pane ef cre ee pee eee ae pare et Eee! poreaeccy PRATICAL 10 Problem statement: Create a Series object ‘employee’ that stores salary of 7 employees. Write script to print 1. Total no of elements 2. Series is empty or not 3. Series consist NaN value or not 4. Count Non-NA elements 5. Axis labels: Solution: Source Code: import pandas as pd D= {'xam':34000, ‘hari’ :42000, ‘suman’: 30000, 'chandan' : 45000, ‘raghu' :23000} employee = pd. Series (D) print (employee) print ("Total no of Employees" employee. size) if employee.empty: print("Series is empty") else: print ("Series is not empty") if employee-hasnans: print ("Series contains NaN elements") else: print ("Series does not contains NaN elements") print("Total no of Non NA elements ",employee.count()} print ("Axis labels\n", employee.axes) Screenshot: Sete P-1agy -CAheedrirser DeLorme hon ihersMSeie PH py 7) =~ a x File fee Forme Ran Options lindow He err | Pte no tee OMe a EEL} Cote tsct5) Peace stor) Print ("Total no of Employees" employee.size) Fee eet print ("Series is empty") ee print ("series is not empty") Peace seen Poem eres Rice TCO CCS CL) ee tC Cones Ceo) Estee ares ae sone scree iy) ata uerh bec Oui street) Output C: \Users\admin\AppData\Local \Programs\Fython\rython310\series y ca EOD hari 42000 suman 30000 Bec ee Coo) peta] 23000 os BELT | Total no of Employees 5 Seer ei ae sg Series does not contains NaN elements Erte Cran a eo} rch gee). sn) [tndex(['ram', ‘hari', 'suman', ‘chandan', 'raghu'], dtype='object'}] | PRATICAL 11 Problem statement: Create the following dataframe ‘Sport’ containing sport wise marks for five students. Use 2D dictionary to create dataframe. Student Sport Marks 1 Cricket 80 1 Football 76 MU Tennis 89 wv Kabaddi 92 V Chandu Hockey 97 Solution: Source Code: import pandas as pd D = {'student':['jai','raj','john','karan', 'chandu'], "sport! :['cricket','football', 'tennis', 'kabaddi', 'hockey'], ‘marks’ : [80,76,89,92,971} sport = pd.DataFrame({D, ['I',‘II',‘III',"IV’,"V"]) print (sport) Screenshot: ee ore! D = {'student!:['jai','xaj','john','karan','chandu'], ‘sport':['cricket', ‘football’, 'tennis', 'kabaddi', 'hockey'], pits EUR rae KAD sport = pd.DataFrame(D, ['I','II',‘III','Iv','v']) print (sport) Output PRATICAL 12 Problem statement: Create a dataframe from list containing dictionaries of most economical bike with its name and rate of three companies. Company name should be the row labels. Solution: Source Code: import pandas as pd Ll = {'Name': 'Sports', 'Cost' :60000} L2 = {'Name': ‘Discover’ , ‘Cost’ : 62000} L3 = {'Name':'splendor' , ‘Cost! : 63000} Bike = [L1,L2,1L3] df = pd.DataFrame (Bike, ['TVS', ‘Bajaj’, 'Hero']) print (df) Screenshot: Pee rae eee Ll = {'Name':'Sparts', 'Cost! :60000} RC Gb URE Se Coty tem rg EL Paes) ZS = {'Name':'splendor’ , ‘cos Cry Fras ES Wee 3] prepa Meeks t(-t e a O-Cs Perea Cra) rary Seay PER recone yer PRATICAL 13 Problem statement: Consider two series object staff and salaries that stores the number of people in various office branches and salaries distributed in these branches respectively. Write a program to create another Series object that stores average salary per branch and then create a dataframe object from these Series object. After creating dataframe rename all row labels with Branch name, Solution: Source Code: import pandas as pd staff = pd.Series([20,24,30,18]) salary = pd. Series ([240000, 336000, 450000,270000]) avg = salary/staff org = {'Employees' :staff, ‘Amount’ : salary, ‘Average df = pd.DataFrame (org) print ("Without Row Label") print (df) df.index = ['sale','store', 'marketing', 'maintenence'] print ("With Row Label") print (d£) Screenshot ence staff = pd/Series([20,24,30,18]) | salary = pd! Series ([240000, 336000, 450000, 270000]) Javg = salary/staft Foc ee SRN ea ee ae er We ee et Chae eer Crs) posed GFR Scart Co oS See Neer] af£lindex = ['sale','store','marketing', 'maintenence'] pore he ek Grae AC) print (df) Output Pec pe Re er 20 240000 24 336000 30 450000 Meet) CFR Ce oS omc ery 20 aes ey Resse] 30 Pere ct 18 > Fureay1) 2000.0 14000.0 15000.0 15000.0 Ferhat 240000 ERI) Ty EXELL) Forest y 12000.0 pe OM) 15000.0 15000.0 PRATICAL 14 Problem statement: Create the following dataframe ‘sales’ containing year wise sales figure for five sales persons in INR. Use the year as column labels, and sales person names as row labels. 2014 «2015 2016 2017 Madhu 1000 2000 2400 2800 Kusum 1500 1800 5000 6000 Kinshuk 2000 2200 7000 7000 Ankit 3000 3000 1000 8000 Shruti 4000 4500 1250 9000 Write program to do the followings 1. Display row labels of ‘sales’ 2. Display column label of ‘sales’ 3. Display last two rows of the ‘sales’ 4. Display first two rows of the ‘sales’. Solution: Source Code: import pandas as pd D = {2014:[1000,1500,2000,3000, 4000] , 2015: [2000,1800,2200, 3000, 4500], 2016: [2400,5000,7000,1000,1250] , 2017: [2800, 6000, 7000,8000,9000]} sale = pd,DataFrame(D, ['Madhu', 'Kusum', 'Kinshuk', ‘Ankit’, ’Shruti']) print ("----DataFrame----" print (sale) print ("----Row Labels----") print (sale. index) print ("----Column Labels----") print (sale. columns) print("----Bottom two Rows----") print (sale. tail(2)) print ("----Top two Rows----"} print (sale.head(2)) Screenshot: Wl import Pec Com ene D = {2014:[1000,1500,2000, 3000, 4000] ,2015:[2000,1800, 2016: [2400, 5000, 7000,1000,1250]', 2017: [2800, 6000, 7000,¢ Peete tee Met Den OT tO kT ot) ec) Se eee print {sale} ost) Beet index) Cote IaL oe print {sale.columns) print ({"----Bottom two Rows----") ise eC p) Top two Rows----") Ele ae 2400 2800 5000 6000 ll eT) Pinte 1000 8000 Preaek 1250 9000 ee ELCs Index({['Madhu', 'Kusum', 'Kinshuk', 'Ankit', ‘Shruti'], dt Re IR oat CoD See Re TEA aL Int64Index([2014, 2015, a Petr aa) er 2014 2015 2016 2017 CECE RL mL) 4000 4500 1250 9000 2014 Plena Madhu 1000 PLCs See ELL) 5000 6000 oes PRATICAL 15 Problem statement: Create a dataframe ‘sales2’ using dictionary as given below and write a program to append ‘sales2’ to the dataframe ‘sales’ created in previous practical 14. 2018 Madhu 1600 Kusum 1100 Kinshuk 5000 Ankit 3400 Shruti 9000 Solution: Source Code: import pandas as pd D = {20143 [1000,1500 , 2000, 3000 , 4000] ,2015: [2000, 1800, 22003000, 4500], 2016: [2400,5000,7000 ,1000,1250] , 2017: [2800, 6000, 7000, 8000,9000) } sale = pd.DataFrame(D,['Madhu', ‘Kusum', 'Kinshuk', ‘Ankit’ , ‘Shruti ']} print ( DataFrame----") print (sale) sale2 = pd.DataFrame ({2018: [1600,1100,5000,5400,9000] }, ['Madhu' , 'Kusum', 'Kinshuk 1, 'Ankit', 'Shruti']) print (sale2) sale = sale.join(sale2) print (sale) Screenshot: Loins. ese EL Lee ore Cl eee ee ere Ce CI 2016: [2400, 5000, 7000, 1000,1250] , 2017: [2800, 6000, 7000, 8000, 9000] } pd_DateFrame{D,[‘Madhu', ‘Kusum', ‘Kinshuk! , 'Ankit', ‘shruti']) PrN tet act wk et] print (sale) ECC eerie Leer Eee eee el ber Re Cae [bce Ae ree Ona eho) Ue ee Se eae RD) print ("----pataFrame Sale2----") Pret eosi 3) sale = sale.join(sale2) peng Se eee ta Sab C ret pec ey eee print (sale) ape) 2014 vets hE Role 1500 Kinshuk 2000 FX ch ECDL) Shruti 4000 Stee ty 2018 Pac eA) Fran ee) Kinshuk 5000 Ankit 5400 Err aE CLO Peemriole wer WC Ee tte t (re wet Ber St Yo 2014 2015 2016 2017) 2018 Madhu 1000 2000 2400 2800 1600 1500 1800 5000 6000 1100 PLT EE ML LC EL) 3000 3000 1000 000 5400 4000 4500 1250 9000 9000 PRATICAL 19 Problem statement: Create a dataframe ‘aid’ as given below and write program to write the values of ‘aid’ to a comma separated file ‘aidfigures.csv’ on the disk. Do not write the row labels and column labels. Toys Books Shoes MP 7000 4300 6000 up 3400 3200 1200 AP 7800 5600 3280 CG 4100 2000 3000 Solution: Source Code: import pandas as pd D = {'Toys':{'MP*:7000, UP’ :3400, 'AB*: 7800, 'CG': 4100}, "Books! : {'MP': 4300, 'UP':3200, 600, "CG" :2000}, "Shoes! : {*MP'; 6000, 'UP';1200, 280, *CG" :3000},) aid = pd.DataPrame(D) print (aid) aid.to_csv(path_or_buf = 'C:/sample/aidfigures.csv', header = False, index = False) Screenshot: ee eae ie ee eee ‘Books preety) print (aid) Pree AC tn a re eed er ton YE ERs he ttt ee a Pr Joo 3400 7800 4100 + Name “TE Favorites © Desktop GQ) sidfigures 3B Lisraies BB Bocuments a Music I stigces - Netepe Wie tat Formn View Hep Foon4a00,6000 B400,3200,1200 7800,5600,2280 4100,2000,3060 PRATICAL 20 Problem statement: Read the data in the file ‘aidfigure.csv’ into a dataframe ‘aidretrieved’ and display it. Now update the row labels and column labels of ‘aidretrieved’ to be the same as that of ‘aid’ of practical 19. Toys Books Shoes MP 7000 4300 6000 UP 3400 3200 1200 AP 7800 5600 3280 cG 4100 2000 3000 Solution: Source Code: import pandas as pd aidretrieved = pd.read_csv('C:/sample/aidfigures.csv', names=['Toys', 'Books' , "Shoes'],) aidretrieved.index = ['MP','UP','AP', 'CG'] print (aidretrieved) Screenshot: 1 eee oat eaeropa te Nip a Bes Led Fern see Reet te- that eka) a ese ete tee) aidretzieved_index = ['MP','UP','AP','CG'] print (aidretrieved) Output Re

You might also like