100% found this document useful (2 votes)
191 views

Pandas Assignment

Uploaded by

Akshay Kumar
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
100% found this document useful (2 votes)
191 views

Pandas Assignment

Uploaded by

Akshay Kumar
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/ 11
119178, 947 Pat 10,,Pandas Assignment Day 10pynd - Colaboratory Pandas Assignment Import pandas and numpy with ther aliases Create a variable 14 Seres({ 100,200,300, 400)) 2 = paasertes( 200, 308, 468)) Print, and datatype Using indexing access the element 300 from the series 2 print(atay) What ate the values of index fr series a? print(index) Printivalues) Rangerndex(starteo, stops, stapes) [ee 200 390 423) Change the index to fb] 2 = pe.sentes(( 200, 208, 300, 400), index = ("2") “2 [Access the value inthe series with index print¢at'a"]) Intps:ifeolab research google. com/érva/taFLOIVZret76aT sD 7gACRWCeNW_wuottontMode=tu an 119178, 947 Pat 410,,Pandas Assignment Day 10pyno Sort the values wrt tothe index and print t assort_index(oxis = 0) ‘ype ante Create a new Pandas Series b having index as‘, and and value 800,450,100 and prit it b= pe.sentes(( 680, 459, 100), index = ("e", "€, “ely ‘Append series at the end ofa series 2 = buappendcay Sort the values in descending order of a and print he index ofthe sorted series 2 + ausort_indextonis = @) Pandas DataFrame ntps:ifeolab rosearch google. com/érvel fgFLOIVZref7aTxDTeACRWCeNW_wuottorintMod: Colaboratory at 119178, 947 Pat 10,,Pandas Assignment Day 10pynd - Colaboratory Part 1 Create a pandas dataframe df from the series's' that we used in the last section print the dataframe oF = pa.vatatrane({ 209, 200, 360, 499, #90, 458, 1681) eFsne390) 100 +200 3400 Whats the shape ofthe datafarme (also, what does it imply?) oy Hey! remember shape (7,1) mplles dataframe has 7 rows and 1 column, Whats the index ofthe dataframe, ist same as the series A yep As same a5 the serses printtindex Rangelndex(starte®, stope7, stapes) print the head and tall ofthe dataframe ‘Addiional- (what does head and taliepresent?) © 100 + 200 3400 4 a0 Intps:ifeolab roscarch google. com/drva/taFLOIVZret76aTxD7eACRWCeNW_wuottorntMode=tue 119123, 947 PM 10,,Pandas Assignment Day 10pynd - Colaboratory print(af.nead(2)) print(af.tati(2)) Rename the column ofthe dataframe as points f.renane(colunnss{°2": points"), inplacesTrue) 100 3400 4 800 Create another Seles ute, which contains random names of tuts from [orangemangsapple randomly selected from [orangemangoapple] fruits = (orange, "ango",‘sppte") fruits = pe.series(("orange’,"nango'y'spple']) (Change the index of futs tothe index of dataframe d fruits = pe.oatafrane({#euses* = (orange, “nango',appte"1)) ‘Add this ute series asa new column to the dataftame df with its column name as fru print the head ofthe dataframe to verity Intps:feolab roscarch google. com/érvaltgFLOIVZret76aTuD7eACRWCeNW_wuottorntMode=tue he series should contaln 7 elements, am 1191723, 947 Pat 10,,Pandas Assignment Day 10pynd - Colaboratory efsneaso) Pandas Concatenation Create a datatrame di where the cols are ‘ty :['Chandigar, Deli, Kanpur, ‘Chennaf, ‘Manali ] and Temperature =[15,22, 20, 26,2] 61 = pa.vatafrane({‘esty" + ['chanaigarh', “DeInt', “Kanpur : 1 “chennat", “Manali, Temperature’ 125, 22, 28, 26,-212) Print) printean) Whats the shape of dt etty = antretty") print ity Whats the type of city prantcetty) © chandigarn Intps:ifeolab rosearch google. com/drvaltgFLOIVZret76aTsD7eACRWOeNW_wuottontMode=tu sit 1191728, 947 PM 10,,Pandas Assignment Day 10pynd - Colaboratory Create another datafeame 2 where the columns are ‘ety’ -[BengalaruColmbatore!SrrangamPondichesry] Temperature’ [24353639] “Tenperature’ + [24,35,3,38]}) print the shape of this datafame 2) ‘merge the two dataames together, save in a new dataframe named'83 63 = pauconcat(fet, a2}, axis = 0) © Changer © 2 Kanpur 2» 2 Strangam 36 3 Pondchenty 38 Select the part ofthe datatrame such that it contains cities wherer temp is less then or equal to 20 How mary cities are there? naa[[ctty' Temperature’ I] {B[ Temperature” ] <= 204([ Torpersture’ I Select the part ofthe datatrame such that contains the cties where tempearature greater than or equal to 35 al Terperature"] = 35 Intps:ifeolab rosearch google. com/érvaltgFLOIVZret76aTxD7gACRWCeNW_wuottontMode=tue ant 119123, 947 PM 10,,Pandas Assignment Day 10pynd - Colaboratory Applying functions to columns and creating new columns We need o ereate another column in 3, which contains @ boolean value foreach city to incleste whether it's a union tertory oF not, + HINT: Chandigarh, Pondicherry and Delhi are only 3 union tetitris here cet is.wt(eity) oulist =f print(a) EF a mot in ["cnandtgarh', ut 21st. append(False) ut List-append(true) ue ots (ect Tinsel wetter = if a not fv ["Chandsgaeh’, “Pondicherry”, episee) = utilise 2 Kanpur 2» © Bongalan Py + Coimbatore 36 2 sorangam 36 [rrue, True, False, False, enh") 182 950 False, False, False, False, Trve] Intps:feolab roscarch google. com/érvaltgFLOIVZret76aTuD7eACRWCeNW_wuottorntMode=tue am 1191728, 947 PM 10,,Pandas Assignment Day 10pynd - Colaboratory chty Temperature is_ut temp farentiet © Chansaarh 18 Te 0 + Dati 22 Te ns 3 henna 2 Fal 708 4 Nana 2 Fale ms 0 Bengatan 2 False 82 2 Sirangam 38 Fae 08 2 Pon ty 39 Tue 1022 ‘The temperatures mentioned in Temperature! column ate mentioned in Celsius, we need another column which contains the same In Fahrenhet, HN + Define a function ¢_t0_f which takes input temp in celsius and returns a value with temperature in Fahrenheit + To check: ¢-to_4(10) should return 50. ser C20 Ho) e213 ane = « te_f(cel) print(*%@.47 degrees Celsius Ss equivalent to 40.16 degrees Fahrenbeit™ X(ce1, fahe)) f check function «.te_#(18) fet eto. fle) pre tay e ‘ann = «.to_4(19) Print(*4l.37 degrees Celsius 4s equivalent to X9.1¢ degrees Fahrentest abn) fa list = fet « te.) #2 Gs hay 3 for x An dsl Temperature’) 4s et0-f0) fahr-Tset- append(¢) eal tenp_faremnser' [= fahe_Dist Intps:ifeolab roscarch google. com/érvaltgFLOIVZret76aTsD7gACRWCeNW_wuottontMode=tue ant 1191728, 947 PM 10,,Pandas Assignment Day 10pynd - Colaboratory Indexing and selecting rows in DataFrame Select subset ofthe dataframe d1 such that it contains the cities which are union teritories 64 = eafas[ ts ue']e “ee at Incoxingiror ‘ssceback (nore recent call Ieee) “Spython-Anput-75-07Ce1649°259 40 srry at = sil esey"|-0e8,2,3] trames Jusr/ocal/1sb/pythons.£/s8st-packages/pandas/core/indexig py in validate key_length(seif, key) 790" def validate ey length(setf, key! Sequence[y]} -> Rone Jot Ge en(eey) > sett. nai 3 1B False Indexingerror("Too many andexers") 74 def _geriven tuple_sane-din(self, taps tuple) Selecta subset ofthe dataframe dl auch that it contains the etes which only have temperature above 90 Farenhiet, ft = e9[ast tenp_tarenhiet 996] Select only the ist three rows of the dataframe dl 4 A0ef 1,233] 3 Pontchery 8 Te 1022 Select all the rows and lat wo columns inthe datarame et Aoe{t:A{ts_ut', "tonp_farenhiet* 1] Groupby 1 create 2 catafrane using tctionary of your choice cata = pd.oataFrane( ane’: ['Ankit’, ‘Aishwarya', “Shaurya!, ‘Shivangi" Ty Age’ + 123, 21, 38, 21], University’ = [°BM, "OMI", “OU, “aMU'D, 29 oF = pa.batatrane(aata, colums-{ ‘Age’ }).sun() sen) Intps:ifeolab roscarch google. com/érvaltgFLOIVZret76aTsD7gACRWCeNW_wuottontMode=tuo ont 119123, 947 PM 10,,Pandas Assignment Day 10pynd - Colaboratory ige 94 type: Snes 44 Use Groupby of single colton with angregste count) (2 = data. groupby([ "University" I)[Universsty']-e2unt() Use Groupby of single column with aggregate sin() and atx() #42 = data. groupby(("Age"})-s12e().groupby (Level-8) ax) (43 = data. groupby({ "University" ]).size()-grougby(leve ning) 44 Use Groupby of any 2 columns with a nea) (2 = data-groupby( ("Age") 988((°Age"* 'ne80"1)) ef2.colunms = [-abr_sean"] na nm m0 2» m0 Use Grouply of any 2 colums with aggregate sin() and max() 22's data. groupby( (Age, Mare" I).agg(Cage"? [nin "mae ]},(°Mave's (nin’, “aa D)) ef2.colums = ("age nin", "age sax stage ierst ag Page Covers min’, Snare ))oCAg8" Cain's mae) oniversity Age a) Data Range Create a pandas daterange where starting date is Tst of January.2020 and end dates 1st of April 2021, store it na new variable named’ 8 = pe.date_range(sta 2020-61-81", endo'2621-01-1") pinta Datetinerndex({'2022-01-01', "2020-01-02", "2020-01-05", "2020-01-64", Intps:ifeolab roscarch google. com/érvaltgFLOIVZret76aTsD7gACRWCeNW_wuottontMode=tuo sont 119178, 947 Pat 10,,Pandas Assignment Day 10pynd - Colaboratory types‘datetinesa{ns]", lengthea57, freq'2") Whats the len of a? tents) Whats the ype ofa? eyeeta) Intps:ifeolab roscarch google. com/érvaltgFLOIVZret76aTsD7gACRWCeNW_wuottontMode=tuo wt

You might also like