Skill Lab Exp 4-5-6

You might also like

Download as pdf
Download as pdf
You are on page 1of 14
EXPERIMENT ~ 4 AIM, 1) Write a Pandas program to convert all the string values to upper, lower cases in a given pandas series. Also find the length of the string values. Source Code. import pandas as pd import numpy as np 8 = pdSeries({'X’ 'Y', Z.,‘Aaba, ‘Baca’, np.nan, 'CABA’, None, ‘bird’, horse’, ‘dog']) print(‘Original series.*) print(s) print(’\nConvert all string values of the said Series to upper case") print(sstrupper()) print(*\nConvert al string values of the said Series to lower cases") print(sstrlower()) print("\nLength of the string values of the said Series”) print(sstrlen()) Output Original series: x y Zz ‘Aaba Baca NaN CABA None bird horse SRYHARBKHO 10 dog. type: object Convert all string values of the said Series fo upper case, (sesh. 4 1 oy 2 2 3 AABA 4 BACA 5 NaN 6 CABA 7 None S BIRD 9 HORSE 10 DOG type, object Convert all string values of the said Series to lower case: x y z aba baca NaN caba None bird horse 10 dog type: object Cor FHV e BNO Length of the string values of the said Series, 10 10 10 40 40 NaN 40 NaN 4.0 50 CRI BHABWNHHO 3 e 5 [J aype: ftoncs AIM. 2) Write a Pandas program to remove whitespaces, left sided whitespaces and right sided whitespaces of the string values of a given pandas series. SourceCode » import pandas as pd color = pdindex(|' Green’, Black’, Red ', White, *Pink'}) print(*Original series.") print(colort) print("\nRemove whitespace’) print(color1.strstrip()) print(’\nRemove left sided whitespace”) print(color1 strIstrip()) print("\nRemove Right sided whitespace") print(colorl strrstrip()) Output. Original series. Index({' Green’, ‘Black’ ' Red ', White’, Pink'', dtype='object’) Remove whitespace Index(|'Green’, Black, 'Red’, ‘White’, Pink’, dlypee'object}) Remove lefl sided whitespace Index({Green’, ‘Black ‘Red ', ‘White’, Pink’, dlype~object) nn | Remove Right sided whitespace Index(|' Green’, ‘Black’, ' Red’, 'White’, ' Pink'], dtype='object’ AIM, 3) Write a Pandas program to count of occurrence of a specified substring in a DataFrame column. Source Code. import pandas as pd df « pd.DataFrame({ ‘name_code'’ ['c001"'c002''c022', 'c2002, 'c2222', ‘date_of_birth '.['12/05/2002"16/02/1999"'25/09/1998"'12/02/2022''15/09/1997'], ‘age’. 18.5, 21.2, 22.5, 22, 23] ) print("Original DataFrame.') print(df) print(’\nCount occurrence of 2 in date_of_birth column") dff'count] = list(map(lambda x. x. count('2"), dff'name_code'l)) print(df) output. Original DateFrame: nane_code date_of_birth age ° eet 12/05/2002 18.5 A cee2 16/02/1999 21.2 2 e22 25/09/1998 22.5 3 2002 12/02/2022 22.8 4 2222 15/09/1997 23.6 Count occurrence of 2 in date_of_birth column: name_code date_of_birth age count @ ceet «12/05/2002 18.5 e 1 802 16/02/1999 21.2 2 822 25/09/1998 22.5 3 2602 12/02/2022 22.0 4 2222 15/09/1997 23.0 BRN AIM: 4' rt eae ) Write a Pandas program to swap the cases of a specified character column in a given DataFrame. Source Code . import pandas as pd df = pd DataFrame({ ‘company_code’ Abed’ EFGP, 'zefsalf, 'sdfslew', ‘zekfsdt, ‘sale_amount’, [12348.5, 2333312, 22.5, 25665520, 23.0] ) print(’Original DataFrame,) print(df) print("\nSwapp cases in comapny_code.") dfl'swapped_company_code'] « list(map(lambda x. xswapcase(), dfl'company_codc'l)) print(df) output. original DataFram company_code date_of_sale sale_amount e Abcd 12/05/2602 32348-5 a EFGF 16/82/1999 ae 2 zefsalf 25/09/1998 is ae 3 sdfslew 12/62/2022 PEG 4 zekfsdf 15/09/1997 . Swapp cases in comapny_code: sup company company_code : @ y Abcd efgt 2 EFGE ZEFSALF SDFSLEN 2 zefsalf ssi 3 sdfslew s 4 zekfsdf [5 rows x 4 colunins } EXPERIMENT- 5 AIM. 1) Write a Pandas program to join the two given dataframes along rows and assign all data. sample Code. import pandas as pd student_datal ~ pd.DataFrame( { 'student_id’: ['S1', ‘$2’, 'S3', 'S4', 'S5'], ‘name’; ['Danniella Fenton’, 'Ryder Storey’, 'Bryce Jensen’, 'Ed Bernal’, ‘Kwame Morin’), 'marks': [200, 210, 190, 222, 199]}) student_data2 - pd.DataFrame({ ‘student_id': ['S4', 'S5', ‘SG’, ‘S7', 'S8'], ‘name’, ['Scarlette Fisher’, 'Carla Williamson’, ‘Dante Morse’, 'Kaiser William’, 'Madeeha Preston’), ‘marks’ [201, 200, 198, 219, 201]}) print(*Original DataFrames:") print(student_data1) print(’-—— print(student_data2) prini(\njoin the said two dataframes along rows!) result_data = pd. concat([student_data1, student_data2}) print(result_data) Results Original DataFrames: student_id name marks 0 $1 Danniella Fenton 200 1 $2 Ryder Storey 210 2 $3 BryceJensen 190 3 $4 Ed Bernal 222 ae 85 Kwame Morin 199 student_id name marks 0 $4 Scarlette Fisher 201 $5 Carla Williamson 200 S86 Dante Morse 198 S87 Kaiser William 219 $8 Madecha Preston 201 Join the said two dataframes along rows: student_id name marks 0 $1 Danniella Fenton 200 $82 Ryder Storey 210 $3 BryceJensen 190 $4 EdBernal 222 $5 Kwame Morin 199 S4 Scarlette Fisher 201 $5 Carla Williamson 200 $6 Dante Morse 198 $7 Kaiser William 219 $8 Madeeha Preston 201 AIM: 2) Write of dictio a Pi dictionerie a ‘andas program to append a list ictioneri existing DataFrame and display the combined data. Sample Code . import pandas as pd student_datal - pd.DataFrame({ ‘stud I ['S1" '$2". "93" 's4" " : ic lent_id’, 'S1", ‘$2’, 83','S4’,'$5'],name’, [Darmiella Fenton’, ‘Ryder Storey’ ‘Bryce Jensen’, 'Ed Bernal’, ‘Kwame Motrin’, ‘marks’. [200, 210, 190, 222, 1991}) id’, ‘name’, 'marks']) 86 = pd Series({'S6’, 'Scarlette Fisher’, 205], index-['student_i dicts « [{'student_id’, 'S6', ‘name’, 'Scarlette Fisher’, 'marks' 203}, {'student_id’. ‘ST’. ‘name’: ‘Bryce Jensen’, ‘marks’. 207}] print("Original DataFrames:’ print(student_data1) print("\nDictionary:") print(s6) combined_data = student_datal append(dicts, ignore_index-True, sort-False) print(*\nCombined Data) print(combined_data) output: original DataFrames: student_id name marks 0 $1 Danniella Fenton 200 1 $2 Ryder Storey 210 2 83 Bryce Jensen 190 3 84 Ed Bernal 222 4 85 Kwame Morin 199 Dictionary: student_id sé name Scarlette Fisher 205 marks St ype: object Combined pata: student iq” [ Sl p. name marks 3 go Daniella Fenton 200 2 ea Ryder Storey 210 3 84 Bryce Jensen 190 s5 Ed Bernal 222 - S6 Ss. Kwame Morin 199 e 87 coriette Fisher 203 Bryce Jensen 207 AIM. 3) Write a Pandas program to join the two dataframes with matching records from both sides where available. Sample Code. import pandas as pd student_datal - pd.DataFrame({ ‘student_id’, ST’, '$2','83,'$4",'S51], ‘name’, [Danniella Fenton’, Ryder Storey’, 'Bryce Jensen’, 'Ed Bernal’, ‘Kwame Morin'], ‘marks’, [200, 210, 190, 222, 199]}) student_data2 = pd.DataFrame({ ‘student_id', ['S4’, 'S5''S6., ‘$7’, 'S81. sname' [Scarlette Fisher’, ‘Carla Williamson: ‘Dante Morse, Kaiser William, ‘Madecha Preston'], ‘marks’: [201, 200, 198, 219, 201]}) print("Original DataFrames") print(student_datal) print(student_data2) merged_data ~ pdmenge(stuc®™ print(’Merged data (outer join)-") print(merged_data) student_data2, on-'student_id’, how-‘outer’) tudent_datal, Output. Original DataFrames: student_id name marks 0 S1 Danniella Fenton 200 1 82 Ryder Storey 210 2 s3 Bryce Jensen 190 3 sa Ed Bernal 222 4 55 Kwame Morin 199 student_id name marks 0 S4 Scarlette Fisher 201 1 SS Carla Williamson 200 2 56 Dante Morse 198 3 S7 Kaiser William 219 4 S8 Madeeha Preston 201 Merged data (outer join): student_id 0 S1 Daniella Fenton 1 s2 Ryder Storey 2 s3 Bryce Jensen 3 sa Ed Bernal 4 $5 Kwame Morin 5 S6 Natt 6 s7 Natl 7 58 Natt name_x marks_x 200.0 NaN 210.0 NaN 190.0 NaN 222.0 Scarlette Fisher 199.0 Carla Williamson NaN Dante Morse NaN Kaiser William NaN Madeeha Preston name_y marks_y NaN NaN NaN 201.0 200.0 198.0 219.0 201.0 .. EXPERIMENT — 6 AIM . Pandas Time Series, 1) Write a Pandas program to create a) Datetime object for Jan 15 2012. b) Specific date and time of 9.20 pm. ©) Local date and time. 4) A date without time. ¢) Current date. f) Time from a datetime. ) Current local time Source Code, import datetime from datetime import datetime Print(‘Datetime object for Jan 11 2012,') print(datetime(2012, 1, 11)) print(’\nSpecific date and time of 9.20 pm’) print (datetime(2011, 1, 11, 21, 20)) print("\nLocal date and time.") print(datetime.now()) print('\nA date without time, " print (datetime.date(datetime(2012, 5, 22))) PANDAS ee eee print¢\ncurrent dates’) print(dattimesnow) date) print(¢\nTime from a dat print(datetime.time(dat print( print (datetime.now()-time()) \nCurrent local times") tetimes") ctime(2012, 12, 15, 18, 12))) Output. Datetime object for Jan 11 2012: 2012-01-11 00:00:00 Specific date and time of 9:20 pm 2014-01-11 21:20:00 Local date and time: 2022-04-07 05:11:41.532893 A date without time: 2012-05-22 Current date 2022-04-07 Time from a datetime: 18:12:00 Current local cime: 05:11:41.533001 AIM, 2) Write a Pandas program fo create a date from a given year, month, day and another date from a given string formats. SourceCode. from datetime import datetime date - datetime(year-2020, month-12, day=25) print("Date from a given year, month, day:’) print(date1) from dateutil import parser date2 - parser-parse("1st of January, 2021") print("\nDate from a given string formats”) print(date2) Output: Date from a given year, month, day: 2020-12-25 00:00:00 Date from a given string formats: 2021-01-04 00:00:00 PANDAS ou — ih. 3) Wiite a Pandas program to cree a tie. seties with two ay random vabues. Kso print the type of the index syster Cote. import pandas 28 94 ingot eae a8 1p import datetime from datetinne imyort datetime, date aes - Aatetime(2011,9, 1), dasetimnel201 1,9, 2)) print Time-series with two index Wabeis*) time_veries - ph Setienp xerdomsantin(2), dates) pried Siene_veries) print aS pg of the index") Printttypeftime_veries index) Output. Tine-series with ono index labels: WO13-09-01 -1-092671 WNs-09-02 0.299422 Orype: floaces Type of che index: | iF PANDAS

You might also like