100% found this document useful (2 votes)
238 views11 pages

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)
238 views11 pages

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 an119178, 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 at119178, 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=tue119123, 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, am1191723, 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 sit1191728, 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” ] 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 ont119123, 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 sont119178, 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