Talk given at RMACC August 17, 2017 titled "Practical Data Wrangling in Pandas".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3_dataframe_operations.ipynb 196 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "** NAVIGATION **\n",
  8. "\n",
  9. "**Got Pandas? _Practical Data Wrangling with Pandas_**\n",
  10. "\n",
  11. "* [Introduction](./0_introduction.ipynb)\n",
  12. "1. [Data Structures](./1_data_structures.ipynb)\n",
  13. "2. [Importing Data](./2_importing_data.ipynb)\n",
  14. "3. **Manipulating DataFrames**\n",
  15. "4. [Wrap Up](./4_wrapping_up.ipynb)\n",
  16. "---"
  17. ]
  18. },
  19. {
  20. "cell_type": "markdown",
  21. "metadata": {
  22. "toc": "true"
  23. },
  24. "source": [
  25. "# Table of Contents\n",
  26. " <p><div class=\"lev1 toc-item\"><a href=\"#Manipulating-DataFrames\" data-toc-modified-id=\"Manipulating-DataFrames-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Manipulating DataFrames</a></div><div class=\"lev2 toc-item\"><a href=\"#More-Selecting\" data-toc-modified-id=\"More-Selecting-11\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>More Selecting</a></div><div class=\"lev3 toc-item\"><a href=\"#The-convenient--[]-operator-(again)\" data-toc-modified-id=\"The-convenient--[]-operator-(again)-111\"><span class=\"toc-item-num\">1.1.1&nbsp;&nbsp;</span>The convenient <code>[]</code> operator (<em>again</em>)</a></div><div class=\"lev3 toc-item\"><a href=\"#Selecting-data-by--.-selector-on-column-and-index-name\" data-toc-modified-id=\"Selecting-data-by--.-selector-on-column-and-index-name-112\"><span class=\"toc-item-num\">1.1.2&nbsp;&nbsp;</span>Selecting data by <code>.</code> selector on column and index name</a></div><div class=\"lev3 toc-item\"><a href=\"#Boolean-selecting\" data-toc-modified-id=\"Boolean-selecting-113\"><span class=\"toc-item-num\">1.1.3&nbsp;&nbsp;</span>Boolean selecting</a></div><div class=\"lev2 toc-item\"><a href=\"#Sorting\" data-toc-modified-id=\"Sorting-12\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>Sorting</a></div><div class=\"lev2 toc-item\"><a href=\"#DataFrame-manipulation\" data-toc-modified-id=\"DataFrame-manipulation-13\"><span class=\"toc-item-num\">1.3&nbsp;&nbsp;</span>DataFrame manipulation</a></div><div class=\"lev3 toc-item\"><a href=\"#Adding-and-dropping-columns\" data-toc-modified-id=\"Adding-and-dropping-columns-131\"><span class=\"toc-item-num\">1.3.1&nbsp;&nbsp;</span>Adding and dropping columns</a></div><div class=\"lev3 toc-item\"><a href=\"#Adding-and-dropping-rows\" data-toc-modified-id=\"Adding-and-dropping-rows-132\"><span class=\"toc-item-num\">1.3.2&nbsp;&nbsp;</span>Adding and dropping rows</a></div><div class=\"lev2 toc-item\"><a href=\"#Advanced-indexing\" data-toc-modified-id=\"Advanced-indexing-14\"><span class=\"toc-item-num\">1.4&nbsp;&nbsp;</span>Advanced indexing</a></div>"
  27. ]
  28. },
  29. {
  30. "cell_type": "markdown",
  31. "metadata": {},
  32. "source": [
  33. "---"
  34. ]
  35. },
  36. {
  37. "cell_type": "markdown",
  38. "metadata": {},
  39. "source": [
  40. "# Manipulating DataFrames"
  41. ]
  42. },
  43. {
  44. "cell_type": "markdown",
  45. "metadata": {},
  46. "source": [
  47. "We will review our terminology for a quick moment:\n",
  48. "* **index** : the column and row indices of your Series or DataFrame, the index for each of these may be hiearchical\n",
  49. " * row index : the index along the horizontal dimension, and typically used as the primary index\n",
  50. " * column index : the index along the vertical dimension\n",
  51. " \n",
  52. " \n",
  53. "* **axis** : the numeric designation for the _column_ or _row_ indices; typically `0` is the _column-axis_ and `1` is the _row-axis_. When dealing with multi-indices, the hierarchy within the axis are referred to as _levels_ and accessed similarly \n",
  54. " \n",
  55. " \n",
  56. "**NOTEBOOK OBJECTIVES**\n",
  57. "\n",
  58. "In this notebook we'll:\n",
  59. "\n",
  60. "* explore more complex slicing and selecting, \n",
  61. "* look at DataFrame concatenation and appending,\n",
  62. "* explore Multi-Indices / hierarchical indexing in Pandas."
  63. ]
  64. },
  65. {
  66. "cell_type": "markdown",
  67. "metadata": {},
  68. "source": [
  69. "## More Selecting\n",
  70. "In the example for this section, we're going to go back to our Baseball data set and load the batting statistics into a DataFrame."
  71. ]
  72. },
  73. {
  74. "cell_type": "code",
  75. "execution_count": 1,
  76. "metadata": {
  77. "collapsed": true
  78. },
  79. "outputs": [],
  80. "source": [
  81. "import pandas as pd\n",
  82. "\n",
  83. "# get the data for players in 2015-16 who played in 100 or more games\n",
  84. "df = pd.read_csv(\"./datasets/Batting.csv\")"
  85. ]
  86. },
  87. {
  88. "cell_type": "markdown",
  89. "metadata": {},
  90. "source": [
  91. "### The convenient `[]` operator (_again_)\n",
  92. "\n",
  93. "As before basic slice selections can be made with the syntax similar to that found in lists using the convenience of the `[]` operator. For example, obtaining the first 5 rows of our data, or the last 15."
  94. ]
  95. },
  96. {
  97. "cell_type": "code",
  98. "execution_count": 2,
  99. "metadata": {},
  100. "outputs": [
  101. {
  102. "data": {
  103. "text/html": [
  104. "<div>\n",
  105. "<style>\n",
  106. " .dataframe thead tr:only-child th {\n",
  107. " text-align: right;\n",
  108. " }\n",
  109. "\n",
  110. " .dataframe thead th {\n",
  111. " text-align: left;\n",
  112. " }\n",
  113. "\n",
  114. " .dataframe tbody tr th {\n",
  115. " vertical-align: top;\n",
  116. " }\n",
  117. "</style>\n",
  118. "<table border=\"1\" class=\"dataframe\">\n",
  119. " <thead>\n",
  120. " <tr style=\"text-align: right;\">\n",
  121. " <th></th>\n",
  122. " <th>playerID</th>\n",
  123. " <th>yearID</th>\n",
  124. " <th>stint</th>\n",
  125. " <th>teamID</th>\n",
  126. " <th>lgID</th>\n",
  127. " <th>G</th>\n",
  128. " <th>AB</th>\n",
  129. " <th>R</th>\n",
  130. " <th>H</th>\n",
  131. " <th>2B</th>\n",
  132. " <th>...</th>\n",
  133. " <th>RBI</th>\n",
  134. " <th>SB</th>\n",
  135. " <th>CS</th>\n",
  136. " <th>BB</th>\n",
  137. " <th>SO</th>\n",
  138. " <th>IBB</th>\n",
  139. " <th>HBP</th>\n",
  140. " <th>SH</th>\n",
  141. " <th>SF</th>\n",
  142. " <th>GIDP</th>\n",
  143. " </tr>\n",
  144. " </thead>\n",
  145. " <tbody>\n",
  146. " <tr>\n",
  147. " <th>0</th>\n",
  148. " <td>abercda01</td>\n",
  149. " <td>1871</td>\n",
  150. " <td>1</td>\n",
  151. " <td>TRO</td>\n",
  152. " <td>NaN</td>\n",
  153. " <td>1</td>\n",
  154. " <td>4</td>\n",
  155. " <td>0</td>\n",
  156. " <td>0</td>\n",
  157. " <td>0</td>\n",
  158. " <td>...</td>\n",
  159. " <td>0.0</td>\n",
  160. " <td>0.0</td>\n",
  161. " <td>0.0</td>\n",
  162. " <td>0</td>\n",
  163. " <td>0.0</td>\n",
  164. " <td>NaN</td>\n",
  165. " <td>NaN</td>\n",
  166. " <td>NaN</td>\n",
  167. " <td>NaN</td>\n",
  168. " <td>NaN</td>\n",
  169. " </tr>\n",
  170. " <tr>\n",
  171. " <th>1</th>\n",
  172. " <td>addybo01</td>\n",
  173. " <td>1871</td>\n",
  174. " <td>1</td>\n",
  175. " <td>RC1</td>\n",
  176. " <td>NaN</td>\n",
  177. " <td>25</td>\n",
  178. " <td>118</td>\n",
  179. " <td>30</td>\n",
  180. " <td>32</td>\n",
  181. " <td>6</td>\n",
  182. " <td>...</td>\n",
  183. " <td>13.0</td>\n",
  184. " <td>8.0</td>\n",
  185. " <td>1.0</td>\n",
  186. " <td>4</td>\n",
  187. " <td>0.0</td>\n",
  188. " <td>NaN</td>\n",
  189. " <td>NaN</td>\n",
  190. " <td>NaN</td>\n",
  191. " <td>NaN</td>\n",
  192. " <td>NaN</td>\n",
  193. " </tr>\n",
  194. " <tr>\n",
  195. " <th>2</th>\n",
  196. " <td>allisar01</td>\n",
  197. " <td>1871</td>\n",
  198. " <td>1</td>\n",
  199. " <td>CL1</td>\n",
  200. " <td>NaN</td>\n",
  201. " <td>29</td>\n",
  202. " <td>137</td>\n",
  203. " <td>28</td>\n",
  204. " <td>40</td>\n",
  205. " <td>4</td>\n",
  206. " <td>...</td>\n",
  207. " <td>19.0</td>\n",
  208. " <td>3.0</td>\n",
  209. " <td>1.0</td>\n",
  210. " <td>2</td>\n",
  211. " <td>5.0</td>\n",
  212. " <td>NaN</td>\n",
  213. " <td>NaN</td>\n",
  214. " <td>NaN</td>\n",
  215. " <td>NaN</td>\n",
  216. " <td>NaN</td>\n",
  217. " </tr>\n",
  218. " <tr>\n",
  219. " <th>3</th>\n",
  220. " <td>allisdo01</td>\n",
  221. " <td>1871</td>\n",
  222. " <td>1</td>\n",
  223. " <td>WS3</td>\n",
  224. " <td>NaN</td>\n",
  225. " <td>27</td>\n",
  226. " <td>133</td>\n",
  227. " <td>28</td>\n",
  228. " <td>44</td>\n",
  229. " <td>10</td>\n",
  230. " <td>...</td>\n",
  231. " <td>27.0</td>\n",
  232. " <td>1.0</td>\n",
  233. " <td>1.0</td>\n",
  234. " <td>0</td>\n",
  235. " <td>2.0</td>\n",
  236. " <td>NaN</td>\n",
  237. " <td>NaN</td>\n",
  238. " <td>NaN</td>\n",
  239. " <td>NaN</td>\n",
  240. " <td>NaN</td>\n",
  241. " </tr>\n",
  242. " <tr>\n",
  243. " <th>4</th>\n",
  244. " <td>ansonca01</td>\n",
  245. " <td>1871</td>\n",
  246. " <td>1</td>\n",
  247. " <td>RC1</td>\n",
  248. " <td>NaN</td>\n",
  249. " <td>25</td>\n",
  250. " <td>120</td>\n",
  251. " <td>29</td>\n",
  252. " <td>39</td>\n",
  253. " <td>11</td>\n",
  254. " <td>...</td>\n",
  255. " <td>16.0</td>\n",
  256. " <td>6.0</td>\n",
  257. " <td>2.0</td>\n",
  258. " <td>2</td>\n",
  259. " <td>1.0</td>\n",
  260. " <td>NaN</td>\n",
  261. " <td>NaN</td>\n",
  262. " <td>NaN</td>\n",
  263. " <td>NaN</td>\n",
  264. " <td>NaN</td>\n",
  265. " </tr>\n",
  266. " </tbody>\n",
  267. "</table>\n",
  268. "<p>5 rows × 22 columns</p>\n",
  269. "</div>"
  270. ],
  271. "text/plain": [
  272. " playerID yearID stint teamID lgID G AB R H 2B ... RBI SB \\\n",
  273. "0 abercda01 1871 1 TRO NaN 1 4 0 0 0 ... 0.0 0.0 \n",
  274. "1 addybo01 1871 1 RC1 NaN 25 118 30 32 6 ... 13.0 8.0 \n",
  275. "2 allisar01 1871 1 CL1 NaN 29 137 28 40 4 ... 19.0 3.0 \n",
  276. "3 allisdo01 1871 1 WS3 NaN 27 133 28 44 10 ... 27.0 1.0 \n",
  277. "4 ansonca01 1871 1 RC1 NaN 25 120 29 39 11 ... 16.0 6.0 \n",
  278. "\n",
  279. " CS BB SO IBB HBP SH SF GIDP \n",
  280. "0 0.0 0 0.0 NaN NaN NaN NaN NaN \n",
  281. "1 1.0 4 0.0 NaN NaN NaN NaN NaN \n",
  282. "2 1.0 2 5.0 NaN NaN NaN NaN NaN \n",
  283. "3 1.0 0 2.0 NaN NaN NaN NaN NaN \n",
  284. "4 2.0 2 1.0 NaN NaN NaN NaN NaN \n",
  285. "\n",
  286. "[5 rows x 22 columns]"
  287. ]
  288. },
  289. "execution_count": 2,
  290. "metadata": {},
  291. "output_type": "execute_result"
  292. }
  293. ],
  294. "source": [
  295. "df[:5]"
  296. ]
  297. },
  298. {
  299. "cell_type": "code",
  300. "execution_count": 3,
  301. "metadata": {},
  302. "outputs": [
  303. {
  304. "data": {
  305. "text/html": [
  306. "<div>\n",
  307. "<style>\n",
  308. " .dataframe thead tr:only-child th {\n",
  309. " text-align: right;\n",
  310. " }\n",
  311. "\n",
  312. " .dataframe thead th {\n",
  313. " text-align: left;\n",
  314. " }\n",
  315. "\n",
  316. " .dataframe tbody tr th {\n",
  317. " vertical-align: top;\n",
  318. " }\n",
  319. "</style>\n",
  320. "<table border=\"1\" class=\"dataframe\">\n",
  321. " <thead>\n",
  322. " <tr style=\"text-align: right;\">\n",
  323. " <th></th>\n",
  324. " <th>playerID</th>\n",
  325. " <th>yearID</th>\n",
  326. " <th>stint</th>\n",
  327. " <th>teamID</th>\n",
  328. " <th>lgID</th>\n",
  329. " <th>G</th>\n",
  330. " <th>AB</th>\n",
  331. " <th>R</th>\n",
  332. " <th>H</th>\n",
  333. " <th>2B</th>\n",
  334. " <th>...</th>\n",
  335. " <th>RBI</th>\n",
  336. " <th>SB</th>\n",
  337. " <th>CS</th>\n",
  338. " <th>BB</th>\n",
  339. " <th>SO</th>\n",
  340. " <th>IBB</th>\n",
  341. " <th>HBP</th>\n",
  342. " <th>SH</th>\n",
  343. " <th>SF</th>\n",
  344. " <th>GIDP</th>\n",
  345. " </tr>\n",
  346. " </thead>\n",
  347. " <tbody>\n",
  348. " <tr>\n",
  349. " <th>102801</th>\n",
  350. " <td>ynoaga01</td>\n",
  351. " <td>2016</td>\n",
  352. " <td>1</td>\n",
  353. " <td>NYN</td>\n",
  354. " <td>NL</td>\n",
  355. " <td>10</td>\n",
  356. " <td>3</td>\n",
  357. " <td>0</td>\n",
  358. " <td>0</td>\n",
  359. " <td>0</td>\n",
  360. " <td>...</td>\n",
  361. " <td>0.0</td>\n",
  362. " <td>0.0</td>\n",
  363. " <td>0.0</td>\n",
  364. " <td>0</td>\n",
  365. " <td>0.0</td>\n",
  366. " <td>0.0</td>\n",
  367. " <td>0.0</td>\n",
  368. " <td>0.0</td>\n",
  369. " <td>0.0</td>\n",
  370. " <td>0.0</td>\n",
  371. " </tr>\n",
  372. " <tr>\n",
  373. " <th>102802</th>\n",
  374. " <td>ynoami01</td>\n",
  375. " <td>2016</td>\n",
  376. " <td>1</td>\n",
  377. " <td>CHA</td>\n",
  378. " <td>AL</td>\n",
  379. " <td>23</td>\n",
  380. " <td>0</td>\n",
  381. " <td>0</td>\n",
  382. " <td>0</td>\n",
  383. " <td>0</td>\n",
  384. " <td>...</td>\n",
  385. " <td>0.0</td>\n",
  386. " <td>0.0</td>\n",
  387. " <td>0.0</td>\n",
  388. " <td>0</td>\n",
  389. " <td>0.0</td>\n",
  390. " <td>0.0</td>\n",
  391. " <td>0.0</td>\n",
  392. " <td>0.0</td>\n",
  393. " <td>0.0</td>\n",
  394. " <td>0.0</td>\n",
  395. " </tr>\n",
  396. " <tr>\n",
  397. " <th>102803</th>\n",
  398. " <td>ynoara01</td>\n",
  399. " <td>2016</td>\n",
  400. " <td>1</td>\n",
  401. " <td>COL</td>\n",
  402. " <td>NL</td>\n",
  403. " <td>3</td>\n",
  404. " <td>5</td>\n",
  405. " <td>0</td>\n",
  406. " <td>0</td>\n",
  407. " <td>0</td>\n",
  408. " <td>...</td>\n",
  409. " <td>0.0</td>\n",
  410. " <td>0.0</td>\n",
  411. " <td>0.0</td>\n",
  412. " <td>0</td>\n",
  413. " <td>2.0</td>\n",
  414. " <td>0.0</td>\n",
  415. " <td>0.0</td>\n",
  416. " <td>0.0</td>\n",
  417. " <td>0.0</td>\n",
  418. " <td>0.0</td>\n",
  419. " </tr>\n",
  420. " <tr>\n",
  421. " <th>102804</th>\n",
  422. " <td>youngch03</td>\n",
  423. " <td>2016</td>\n",
  424. " <td>1</td>\n",
  425. " <td>KCA</td>\n",
  426. " <td>AL</td>\n",
  427. " <td>34</td>\n",
  428. " <td>1</td>\n",
  429. " <td>0</td>\n",
  430. " <td>0</td>\n",
  431. " <td>0</td>\n",
  432. " <td>...</td>\n",
  433. " <td>0.0</td>\n",
  434. " <td>0.0</td>\n",
  435. " <td>0.0</td>\n",
  436. " <td>0</td>\n",
  437. " <td>0.0</td>\n",
  438. " <td>0.0</td>\n",
  439. " <td>0.0</td>\n",
  440. " <td>0.0</td>\n",
  441. " <td>0.0</td>\n",
  442. " <td>0.0</td>\n",
  443. " </tr>\n",
  444. " <tr>\n",
  445. " <th>102805</th>\n",
  446. " <td>youngch04</td>\n",
  447. " <td>2016</td>\n",
  448. " <td>1</td>\n",
  449. " <td>BOS</td>\n",
  450. " <td>AL</td>\n",
  451. " <td>76</td>\n",
  452. " <td>203</td>\n",
  453. " <td>29</td>\n",
  454. " <td>56</td>\n",
  455. " <td>18</td>\n",
  456. " <td>...</td>\n",
  457. " <td>24.0</td>\n",
  458. " <td>4.0</td>\n",
  459. " <td>2.0</td>\n",
  460. " <td>21</td>\n",
  461. " <td>50.0</td>\n",
  462. " <td>0.0</td>\n",
  463. " <td>3.0</td>\n",
  464. " <td>0.0</td>\n",
  465. " <td>0.0</td>\n",
  466. " <td>4.0</td>\n",
  467. " </tr>\n",
  468. " <tr>\n",
  469. " <th>102806</th>\n",
  470. " <td>younger03</td>\n",
  471. " <td>2016</td>\n",
  472. " <td>1</td>\n",
  473. " <td>NYA</td>\n",
  474. " <td>AL</td>\n",
  475. " <td>6</td>\n",
  476. " <td>1</td>\n",
  477. " <td>2</td>\n",
  478. " <td>0</td>\n",
  479. " <td>0</td>\n",
  480. " <td>...</td>\n",
  481. " <td>0.0</td>\n",
  482. " <td>1.0</td>\n",
  483. " <td>0.0</td>\n",
  484. " <td>0</td>\n",
  485. " <td>0.0</td>\n",
  486. " <td>0.0</td>\n",
  487. " <td>0.0</td>\n",
  488. " <td>0.0</td>\n",
  489. " <td>0.0</td>\n",
  490. " <td>0.0</td>\n",
  491. " </tr>\n",
  492. " <tr>\n",
  493. " <th>102807</th>\n",
  494. " <td>youngma03</td>\n",
  495. " <td>2016</td>\n",
  496. " <td>1</td>\n",
  497. " <td>ATL</td>\n",
  498. " <td>NL</td>\n",
  499. " <td>8</td>\n",
  500. " <td>0</td>\n",
  501. " <td>0</td>\n",
  502. " <td>0</td>\n",
  503. " <td>0</td>\n",
  504. " <td>...</td>\n",
  505. " <td>0.0</td>\n",
  506. " <td>0.0</td>\n",
  507. " <td>0.0</td>\n",
  508. " <td>0</td>\n",
  509. " <td>0.0</td>\n",
  510. " <td>0.0</td>\n",
  511. " <td>0.0</td>\n",
  512. " <td>0.0</td>\n",
  513. " <td>0.0</td>\n",
  514. " <td>0.0</td>\n",
  515. " </tr>\n",
  516. " <tr>\n",
  517. " <th>102808</th>\n",
  518. " <td>zastrro01</td>\n",
  519. " <td>2016</td>\n",
  520. " <td>1</td>\n",
  521. " <td>CHN</td>\n",
  522. " <td>NL</td>\n",
  523. " <td>8</td>\n",
  524. " <td>3</td>\n",
  525. " <td>0</td>\n",
  526. " <td>0</td>\n",
  527. " <td>0</td>\n",
  528. " <td>...</td>\n",
  529. " <td>0.0</td>\n",
  530. " <td>0.0</td>\n",
  531. " <td>0.0</td>\n",
  532. " <td>0</td>\n",
  533. " <td>2.0</td>\n",
  534. " <td>0.0</td>\n",
  535. " <td>0.0</td>\n",
  536. " <td>0.0</td>\n",
  537. " <td>0.0</td>\n",
  538. " <td>0.0</td>\n",
  539. " </tr>\n",
  540. " <tr>\n",
  541. " <th>102809</th>\n",
  542. " <td>zieglbr01</td>\n",
  543. " <td>2016</td>\n",
  544. " <td>1</td>\n",
  545. " <td>ARI</td>\n",
  546. " <td>NL</td>\n",
  547. " <td>36</td>\n",
  548. " <td>0</td>\n",
  549. " <td>0</td>\n",
  550. " <td>0</td>\n",
  551. " <td>0</td>\n",
  552. " <td>...</td>\n",
  553. " <td>0.0</td>\n",
  554. " <td>0.0</td>\n",
  555. " <td>0.0</td>\n",
  556. " <td>0</td>\n",
  557. " <td>0.0</td>\n",
  558. " <td>0.0</td>\n",
  559. " <td>0.0</td>\n",
  560. " <td>0.0</td>\n",
  561. " <td>0.0</td>\n",
  562. " <td>0.0</td>\n",
  563. " </tr>\n",
  564. " <tr>\n",
  565. " <th>102810</th>\n",
  566. " <td>zieglbr01</td>\n",
  567. " <td>2016</td>\n",
  568. " <td>2</td>\n",
  569. " <td>BOS</td>\n",
  570. " <td>AL</td>\n",
  571. " <td>33</td>\n",
  572. " <td>0</td>\n",
  573. " <td>0</td>\n",
  574. " <td>0</td>\n",
  575. " <td>0</td>\n",
  576. " <td>...</td>\n",
  577. " <td>0.0</td>\n",
  578. " <td>0.0</td>\n",
  579. " <td>0.0</td>\n",
  580. " <td>0</td>\n",
  581. " <td>0.0</td>\n",
  582. " <td>0.0</td>\n",
  583. " <td>0.0</td>\n",
  584. " <td>0.0</td>\n",
  585. " <td>0.0</td>\n",
  586. " <td>0.0</td>\n",
  587. " </tr>\n",
  588. " <tr>\n",
  589. " <th>102811</th>\n",
  590. " <td>zimmejo02</td>\n",
  591. " <td>2016</td>\n",
  592. " <td>1</td>\n",
  593. " <td>DET</td>\n",
  594. " <td>AL</td>\n",
  595. " <td>19</td>\n",
  596. " <td>4</td>\n",
  597. " <td>0</td>\n",
  598. " <td>1</td>\n",
  599. " <td>0</td>\n",
  600. " <td>...</td>\n",
  601. " <td>0.0</td>\n",
  602. " <td>0.0</td>\n",
  603. " <td>0.0</td>\n",
  604. " <td>0</td>\n",
  605. " <td>2.0</td>\n",
  606. " <td>0.0</td>\n",
  607. " <td>0.0</td>\n",
  608. " <td>1.0</td>\n",
  609. " <td>0.0</td>\n",
  610. " <td>0.0</td>\n",
  611. " </tr>\n",
  612. " <tr>\n",
  613. " <th>102812</th>\n",
  614. " <td>zimmery01</td>\n",
  615. " <td>2016</td>\n",
  616. " <td>1</td>\n",
  617. " <td>WAS</td>\n",
  618. " <td>NL</td>\n",
  619. " <td>115</td>\n",
  620. " <td>427</td>\n",
  621. " <td>60</td>\n",
  622. " <td>93</td>\n",
  623. " <td>18</td>\n",
  624. " <td>...</td>\n",
  625. " <td>46.0</td>\n",
  626. " <td>4.0</td>\n",
  627. " <td>1.0</td>\n",
  628. " <td>29</td>\n",
  629. " <td>104.0</td>\n",
  630. " <td>1.0</td>\n",
  631. " <td>5.0</td>\n",
  632. " <td>0.0</td>\n",
  633. " <td>6.0</td>\n",
  634. " <td>12.0</td>\n",
  635. " </tr>\n",
  636. " <tr>\n",
  637. " <th>102813</th>\n",
  638. " <td>zobribe01</td>\n",
  639. " <td>2016</td>\n",
  640. " <td>1</td>\n",
  641. " <td>CHN</td>\n",
  642. " <td>NL</td>\n",
  643. " <td>147</td>\n",
  644. " <td>523</td>\n",
  645. " <td>94</td>\n",
  646. " <td>142</td>\n",
  647. " <td>31</td>\n",
  648. " <td>...</td>\n",
  649. " <td>76.0</td>\n",
  650. " <td>6.0</td>\n",
  651. " <td>4.0</td>\n",
  652. " <td>96</td>\n",
  653. " <td>82.0</td>\n",
  654. " <td>6.0</td>\n",
  655. " <td>4.0</td>\n",
  656. " <td>4.0</td>\n",
  657. " <td>4.0</td>\n",
  658. " <td>17.0</td>\n",
  659. " </tr>\n",
  660. " <tr>\n",
  661. " <th>102814</th>\n",
  662. " <td>zuninmi01</td>\n",
  663. " <td>2016</td>\n",
  664. " <td>1</td>\n",
  665. " <td>SEA</td>\n",
  666. " <td>AL</td>\n",
  667. " <td>55</td>\n",
  668. " <td>164</td>\n",
  669. " <td>16</td>\n",
  670. " <td>34</td>\n",
  671. " <td>7</td>\n",
  672. " <td>...</td>\n",
  673. " <td>31.0</td>\n",
  674. " <td>0.0</td>\n",
  675. " <td>0.0</td>\n",
  676. " <td>21</td>\n",
  677. " <td>65.0</td>\n",
  678. " <td>0.0</td>\n",
  679. " <td>6.0</td>\n",
  680. " <td>0.0</td>\n",
  681. " <td>1.0</td>\n",
  682. " <td>0.0</td>\n",
  683. " </tr>\n",
  684. " <tr>\n",
  685. " <th>102815</th>\n",
  686. " <td>zychto01</td>\n",
  687. " <td>2016</td>\n",
  688. " <td>1</td>\n",
  689. " <td>SEA</td>\n",
  690. " <td>AL</td>\n",
  691. " <td>12</td>\n",
  692. " <td>0</td>\n",
  693. " <td>0</td>\n",
  694. " <td>0</td>\n",
  695. " <td>0</td>\n",
  696. " <td>...</td>\n",
  697. " <td>0.0</td>\n",
  698. " <td>0.0</td>\n",
  699. " <td>0.0</td>\n",
  700. " <td>0</td>\n",
  701. " <td>0.0</td>\n",
  702. " <td>0.0</td>\n",
  703. " <td>0.0</td>\n",
  704. " <td>0.0</td>\n",
  705. " <td>0.0</td>\n",
  706. " <td>0.0</td>\n",
  707. " </tr>\n",
  708. " </tbody>\n",
  709. "</table>\n",
  710. "<p>15 rows × 22 columns</p>\n",
  711. "</div>"
  712. ],
  713. "text/plain": [
  714. " playerID yearID stint teamID lgID G AB R H 2B ... \\\n",
  715. "102801 ynoaga01 2016 1 NYN NL 10 3 0 0 0 ... \n",
  716. "102802 ynoami01 2016 1 CHA AL 23 0 0 0 0 ... \n",
  717. "102803 ynoara01 2016 1 COL NL 3 5 0 0 0 ... \n",
  718. "102804 youngch03 2016 1 KCA AL 34 1 0 0 0 ... \n",
  719. "102805 youngch04 2016 1 BOS AL 76 203 29 56 18 ... \n",
  720. "102806 younger03 2016 1 NYA AL 6 1 2 0 0 ... \n",
  721. "102807 youngma03 2016 1 ATL NL 8 0 0 0 0 ... \n",
  722. "102808 zastrro01 2016 1 CHN NL 8 3 0 0 0 ... \n",
  723. "102809 zieglbr01 2016 1 ARI NL 36 0 0 0 0 ... \n",
  724. "102810 zieglbr01 2016 2 BOS AL 33 0 0 0 0 ... \n",
  725. "102811 zimmejo02 2016 1 DET AL 19 4 0 1 0 ... \n",
  726. "102812 zimmery01 2016 1 WAS NL 115 427 60 93 18 ... \n",
  727. "102813 zobribe01 2016 1 CHN NL 147 523 94 142 31 ... \n",
  728. "102814 zuninmi01 2016 1 SEA AL 55 164 16 34 7 ... \n",
  729. "102815 zychto01 2016 1 SEA AL 12 0 0 0 0 ... \n",
  730. "\n",
  731. " RBI SB CS BB SO IBB HBP SH SF GIDP \n",
  732. "102801 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  733. "102802 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  734. "102803 0.0 0.0 0.0 0 2.0 0.0 0.0 0.0 0.0 0.0 \n",
  735. "102804 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  736. "102805 24.0 4.0 2.0 21 50.0 0.0 3.0 0.0 0.0 4.0 \n",
  737. "102806 0.0 1.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  738. "102807 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  739. "102808 0.0 0.0 0.0 0 2.0 0.0 0.0 0.0 0.0 0.0 \n",
  740. "102809 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  741. "102810 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  742. "102811 0.0 0.0 0.0 0 2.0 0.0 0.0 1.0 0.0 0.0 \n",
  743. "102812 46.0 4.0 1.0 29 104.0 1.0 5.0 0.0 6.0 12.0 \n",
  744. "102813 76.0 6.0 4.0 96 82.0 6.0 4.0 4.0 4.0 17.0 \n",
  745. "102814 31.0 0.0 0.0 21 65.0 0.0 6.0 0.0 1.0 0.0 \n",
  746. "102815 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  747. "\n",
  748. "[15 rows x 22 columns]"
  749. ]
  750. },
  751. "execution_count": 3,
  752. "metadata": {},
  753. "output_type": "execute_result"
  754. }
  755. ],
  756. "source": [
  757. "df[-15:]"
  758. ]
  759. },
  760. {
  761. "cell_type": "markdown",
  762. "metadata": {},
  763. "source": [
  764. "We mostly worked on _row slicing_ with the `[]` selector, but if we pass a _column label_ or **list** of the columns we'd like, say the `RBI` and `G` (games played) data, we get mostly what we'd expect:"
  765. ]
  766. },
  767. {
  768. "cell_type": "code",
  769. "execution_count": null,
  770. "metadata": {
  771. "collapsed": true
  772. },
  773. "outputs": [],
  774. "source": [
  775. "df[\"RBI\"][:5]"
  776. ]
  777. },
  778. {
  779. "cell_type": "code",
  780. "execution_count": null,
  781. "metadata": {
  782. "collapsed": true
  783. },
  784. "outputs": [],
  785. "source": [
  786. "df[[\"RBI\", \"G\"]][:10]"
  787. ]
  788. },
  789. {
  790. "cell_type": "markdown",
  791. "metadata": {},
  792. "source": [
  793. "### Selecting data by `.` selector on column and index name\n",
  794. "\n",
  795. "We can obtain _column_ data by column labels (note that the column index was loaded for us when we read the file into the DataFrame). For example to get all the `RBI` data:"
  796. ]
  797. },
  798. {
  799. "cell_type": "code",
  800. "execution_count": 4,
  801. "metadata": {},
  802. "outputs": [
  803. {
  804. "data": {
  805. "text/plain": [
  806. "0 0.0\n",
  807. "1 13.0\n",
  808. "2 19.0\n",
  809. "3 27.0\n",
  810. "4 16.0\n",
  811. "5 5.0\n",
  812. "6 2.0\n",
  813. "7 34.0\n",
  814. "8 1.0\n",
  815. "9 11.0\n",
  816. "Name: RBI, dtype: float64"
  817. ]
  818. },
  819. "execution_count": 4,
  820. "metadata": {},
  821. "output_type": "execute_result"
  822. }
  823. ],
  824. "source": [
  825. "df.RBI[:10]"
  826. ]
  827. },
  828. {
  829. "cell_type": "markdown",
  830. "metadata": {},
  831. "source": [
  832. "Similarly, we can pass a **list** of the columns we'd like, so let's get the `RBI` and `G` (games played) data:"
  833. ]
  834. },
  835. {
  836. "cell_type": "code",
  837. "execution_count": 5,
  838. "metadata": {},
  839. "outputs": [
  840. {
  841. "data": {
  842. "text/html": [
  843. "<div>\n",
  844. "<style>\n",
  845. " .dataframe thead tr:only-child th {\n",
  846. " text-align: right;\n",
  847. " }\n",
  848. "\n",
  849. " .dataframe thead th {\n",
  850. " text-align: left;\n",
  851. " }\n",
  852. "\n",
  853. " .dataframe tbody tr th {\n",
  854. " vertical-align: top;\n",
  855. " }\n",
  856. "</style>\n",
  857. "<table border=\"1\" class=\"dataframe\">\n",
  858. " <thead>\n",
  859. " <tr style=\"text-align: right;\">\n",
  860. " <th></th>\n",
  861. " <th>RBI</th>\n",
  862. " <th>G</th>\n",
  863. " </tr>\n",
  864. " </thead>\n",
  865. " <tbody>\n",
  866. " <tr>\n",
  867. " <th>0</th>\n",
  868. " <td>0.0</td>\n",
  869. " <td>1</td>\n",
  870. " </tr>\n",
  871. " <tr>\n",
  872. " <th>1</th>\n",
  873. " <td>13.0</td>\n",
  874. " <td>25</td>\n",
  875. " </tr>\n",
  876. " <tr>\n",
  877. " <th>2</th>\n",
  878. " <td>19.0</td>\n",
  879. " <td>29</td>\n",
  880. " </tr>\n",
  881. " <tr>\n",
  882. " <th>3</th>\n",
  883. " <td>27.0</td>\n",
  884. " <td>27</td>\n",
  885. " </tr>\n",
  886. " <tr>\n",
  887. " <th>4</th>\n",
  888. " <td>16.0</td>\n",
  889. " <td>25</td>\n",
  890. " </tr>\n",
  891. " <tr>\n",
  892. " <th>5</th>\n",
  893. " <td>5.0</td>\n",
  894. " <td>12</td>\n",
  895. " </tr>\n",
  896. " <tr>\n",
  897. " <th>6</th>\n",
  898. " <td>2.0</td>\n",
  899. " <td>1</td>\n",
  900. " </tr>\n",
  901. " <tr>\n",
  902. " <th>7</th>\n",
  903. " <td>34.0</td>\n",
  904. " <td>31</td>\n",
  905. " </tr>\n",
  906. " <tr>\n",
  907. " <th>8</th>\n",
  908. " <td>1.0</td>\n",
  909. " <td>1</td>\n",
  910. " </tr>\n",
  911. " <tr>\n",
  912. " <th>9</th>\n",
  913. " <td>11.0</td>\n",
  914. " <td>18</td>\n",
  915. " </tr>\n",
  916. " </tbody>\n",
  917. "</table>\n",
  918. "</div>"
  919. ],
  920. "text/plain": [
  921. " RBI G\n",
  922. "0 0.0 1\n",
  923. "1 13.0 25\n",
  924. "2 19.0 29\n",
  925. "3 27.0 27\n",
  926. "4 16.0 25\n",
  927. "5 5.0 12\n",
  928. "6 2.0 1\n",
  929. "7 34.0 31\n",
  930. "8 1.0 1\n",
  931. "9 11.0 18"
  932. ]
  933. },
  934. "execution_count": 5,
  935. "metadata": {},
  936. "output_type": "execute_result"
  937. }
  938. ],
  939. "source": [
  940. "df[[\"RBI\", \"G\"]][:10]"
  941. ]
  942. },
  943. {
  944. "cell_type": "markdown",
  945. "metadata": {},
  946. "source": [
  947. "### Boolean selecting\n",
  948. "We have yet to make more complex selections beyond index values. Now we're ready to introduce selecting by boolean value. With this kinds of selection, we're going to as Pandas to give us the Series or DataFrame that represents the _boolean_ values of what we want, then we will allow `iloc` to reduce the resulting Series or DataFrame to what we're looking for. Let's see this in action.\n",
  949. "\n",
  950. "Say we want to find all items in our DataFrame where `yearID` is `2015` or\n",
  951. "\n",
  952. "```\n",
  953. "df.yearID == 2015\n",
  954. "```\n",
  955. "\n",
  956. "Let's first see what this does."
  957. ]
  958. },
  959. {
  960. "cell_type": "code",
  961. "execution_count": 6,
  962. "metadata": {},
  963. "outputs": [
  964. {
  965. "data": {
  966. "text/plain": [
  967. "0 False\n",
  968. "1 False\n",
  969. "2 False\n",
  970. "3 False\n",
  971. "4 False\n",
  972. "5 False\n",
  973. "6 False\n",
  974. "7 False\n",
  975. "8 False\n",
  976. "9 False\n",
  977. "10 False\n",
  978. "11 False\n",
  979. "12 False\n",
  980. "13 False\n",
  981. "14 False\n",
  982. "15 False\n",
  983. "16 False\n",
  984. "17 False\n",
  985. "18 False\n",
  986. "19 False\n",
  987. "20 False\n",
  988. "21 False\n",
  989. "22 False\n",
  990. "23 False\n",
  991. "24 False\n",
  992. "25 False\n",
  993. "26 False\n",
  994. "27 False\n",
  995. "28 False\n",
  996. "29 False\n",
  997. " ... \n",
  998. "102786 False\n",
  999. "102787 False\n",
  1000. "102788 False\n",
  1001. "102789 False\n",
  1002. "102790 False\n",
  1003. "102791 False\n",
  1004. "102792 False\n",
  1005. "102793 False\n",
  1006. "102794 False\n",
  1007. "102795 False\n",
  1008. "102796 False\n",
  1009. "102797 False\n",
  1010. "102798 False\n",
  1011. "102799 False\n",
  1012. "102800 False\n",
  1013. "102801 False\n",
  1014. "102802 False\n",
  1015. "102803 False\n",
  1016. "102804 False\n",
  1017. "102805 False\n",
  1018. "102806 False\n",
  1019. "102807 False\n",
  1020. "102808 False\n",
  1021. "102809 False\n",
  1022. "102810 False\n",
  1023. "102811 False\n",
  1024. "102812 False\n",
  1025. "102813 False\n",
  1026. "102814 False\n",
  1027. "102815 False\n",
  1028. "Name: yearID, Length: 102816, dtype: bool"
  1029. ]
  1030. },
  1031. "execution_count": 6,
  1032. "metadata": {},
  1033. "output_type": "execute_result"
  1034. }
  1035. ],
  1036. "source": [
  1037. "df.yearID == 2015"
  1038. ]
  1039. },
  1040. {
  1041. "cell_type": "markdown",
  1042. "metadata": {},
  1043. "source": [
  1044. "We're returned the Series that contains a `True` or `False` given our _boolean_ query. We need now pass this _boolean_ Series into `loc` and we will see the outcome."
  1045. ]
  1046. },
  1047. {
  1048. "cell_type": "code",
  1049. "execution_count": 7,
  1050. "metadata": {},
  1051. "outputs": [
  1052. {
  1053. "data": {
  1054. "text/html": [
  1055. "<div>\n",
  1056. "<style>\n",
  1057. " .dataframe thead tr:only-child th {\n",
  1058. " text-align: right;\n",
  1059. " }\n",
  1060. "\n",
  1061. " .dataframe thead th {\n",
  1062. " text-align: left;\n",
  1063. " }\n",
  1064. "\n",
  1065. " .dataframe tbody tr th {\n",
  1066. " vertical-align: top;\n",
  1067. " }\n",
  1068. "</style>\n",
  1069. "<table border=\"1\" class=\"dataframe\">\n",
  1070. " <thead>\n",
  1071. " <tr style=\"text-align: right;\">\n",
  1072. " <th></th>\n",
  1073. " <th>playerID</th>\n",
  1074. " <th>yearID</th>\n",
  1075. " <th>stint</th>\n",
  1076. " <th>teamID</th>\n",
  1077. " <th>lgID</th>\n",
  1078. " <th>G</th>\n",
  1079. " <th>AB</th>\n",
  1080. " <th>R</th>\n",
  1081. " <th>H</th>\n",
  1082. " <th>2B</th>\n",
  1083. " <th>...</th>\n",
  1084. " <th>RBI</th>\n",
  1085. " <th>SB</th>\n",
  1086. " <th>CS</th>\n",
  1087. " <th>BB</th>\n",
  1088. " <th>SO</th>\n",
  1089. " <th>IBB</th>\n",
  1090. " <th>HBP</th>\n",
  1091. " <th>SH</th>\n",
  1092. " <th>SF</th>\n",
  1093. " <th>GIDP</th>\n",
  1094. " </tr>\n",
  1095. " </thead>\n",
  1096. " <tbody>\n",
  1097. " <tr>\n",
  1098. " <th>99847</th>\n",
  1099. " <td>aardsda01</td>\n",
  1100. " <td>2015</td>\n",
  1101. " <td>1</td>\n",
  1102. " <td>ATL</td>\n",
  1103. " <td>NL</td>\n",
  1104. " <td>33</td>\n",
  1105. " <td>1</td>\n",
  1106. " <td>0</td>\n",
  1107. " <td>0</td>\n",
  1108. " <td>0</td>\n",
  1109. " <td>...</td>\n",
  1110. " <td>0.0</td>\n",
  1111. " <td>0.0</td>\n",
  1112. " <td>0.0</td>\n",
  1113. " <td>0</td>\n",
  1114. " <td>1.0</td>\n",
  1115. " <td>0.0</td>\n",
  1116. " <td>0.0</td>\n",
  1117. " <td>0.0</td>\n",
  1118. " <td>0.0</td>\n",
  1119. " <td>0.0</td>\n",
  1120. " </tr>\n",
  1121. " <tr>\n",
  1122. " <th>99848</th>\n",
  1123. " <td>abadfe01</td>\n",
  1124. " <td>2015</td>\n",
  1125. " <td>1</td>\n",
  1126. " <td>OAK</td>\n",
  1127. " <td>AL</td>\n",
  1128. " <td>62</td>\n",
  1129. " <td>0</td>\n",
  1130. " <td>0</td>\n",
  1131. " <td>0</td>\n",
  1132. " <td>0</td>\n",
  1133. " <td>...</td>\n",
  1134. " <td>0.0</td>\n",
  1135. " <td>0.0</td>\n",
  1136. " <td>0.0</td>\n",
  1137. " <td>0</td>\n",
  1138. " <td>0.0</td>\n",
  1139. " <td>0.0</td>\n",
  1140. " <td>0.0</td>\n",
  1141. " <td>0.0</td>\n",
  1142. " <td>0.0</td>\n",
  1143. " <td>0.0</td>\n",
  1144. " </tr>\n",
  1145. " <tr>\n",
  1146. " <th>99849</th>\n",
  1147. " <td>abreujo02</td>\n",
  1148. " <td>2015</td>\n",
  1149. " <td>1</td>\n",
  1150. " <td>CHA</td>\n",
  1151. " <td>AL</td>\n",
  1152. " <td>154</td>\n",
  1153. " <td>613</td>\n",
  1154. " <td>88</td>\n",
  1155. " <td>178</td>\n",
  1156. " <td>34</td>\n",
  1157. " <td>...</td>\n",
  1158. " <td>101.0</td>\n",
  1159. " <td>0.0</td>\n",
  1160. " <td>0.0</td>\n",
  1161. " <td>39</td>\n",
  1162. " <td>140.0</td>\n",
  1163. " <td>11.0</td>\n",
  1164. " <td>15.0</td>\n",
  1165. " <td>0.0</td>\n",
  1166. " <td>1.0</td>\n",
  1167. " <td>16.0</td>\n",
  1168. " </tr>\n",
  1169. " <tr>\n",
  1170. " <th>99850</th>\n",
  1171. " <td>achteaj01</td>\n",
  1172. " <td>2015</td>\n",
  1173. " <td>1</td>\n",
  1174. " <td>MIN</td>\n",
  1175. " <td>AL</td>\n",
  1176. " <td>11</td>\n",
  1177. " <td>0</td>\n",
  1178. " <td>0</td>\n",
  1179. " <td>0</td>\n",
  1180. " <td>0</td>\n",
  1181. " <td>...</td>\n",
  1182. " <td>0.0</td>\n",
  1183. " <td>0.0</td>\n",
  1184. " <td>0.0</td>\n",
  1185. " <td>0</td>\n",
  1186. " <td>0.0</td>\n",
  1187. " <td>0.0</td>\n",
  1188. " <td>0.0</td>\n",
  1189. " <td>0.0</td>\n",
  1190. " <td>0.0</td>\n",
  1191. " <td>0.0</td>\n",
  1192. " </tr>\n",
  1193. " <tr>\n",
  1194. " <th>99851</th>\n",
  1195. " <td>ackledu01</td>\n",
  1196. " <td>2015</td>\n",
  1197. " <td>1</td>\n",
  1198. " <td>SEA</td>\n",
  1199. " <td>AL</td>\n",
  1200. " <td>85</td>\n",
  1201. " <td>186</td>\n",
  1202. " <td>22</td>\n",
  1203. " <td>40</td>\n",
  1204. " <td>8</td>\n",
  1205. " <td>...</td>\n",
  1206. " <td>19.0</td>\n",
  1207. " <td>2.0</td>\n",
  1208. " <td>2.0</td>\n",
  1209. " <td>14</td>\n",
  1210. " <td>38.0</td>\n",
  1211. " <td>0.0</td>\n",
  1212. " <td>1.0</td>\n",
  1213. " <td>3.0</td>\n",
  1214. " <td>3.0</td>\n",
  1215. " <td>3.0</td>\n",
  1216. " </tr>\n",
  1217. " <tr>\n",
  1218. " <th>99852</th>\n",
  1219. " <td>ackledu01</td>\n",
  1220. " <td>2015</td>\n",
  1221. " <td>2</td>\n",
  1222. " <td>NYA</td>\n",
  1223. " <td>AL</td>\n",
  1224. " <td>23</td>\n",
  1225. " <td>52</td>\n",
  1226. " <td>6</td>\n",
  1227. " <td>15</td>\n",
  1228. " <td>3</td>\n",
  1229. " <td>...</td>\n",
  1230. " <td>11.0</td>\n",
  1231. " <td>0.0</td>\n",
  1232. " <td>0.0</td>\n",
  1233. " <td>4</td>\n",
  1234. " <td>7.0</td>\n",
  1235. " <td>0.0</td>\n",
  1236. " <td>0.0</td>\n",
  1237. " <td>0.0</td>\n",
  1238. " <td>1.0</td>\n",
  1239. " <td>0.0</td>\n",
  1240. " </tr>\n",
  1241. " <tr>\n",
  1242. " <th>99853</th>\n",
  1243. " <td>adamecr01</td>\n",
  1244. " <td>2015</td>\n",
  1245. " <td>1</td>\n",
  1246. " <td>COL</td>\n",
  1247. " <td>NL</td>\n",
  1248. " <td>26</td>\n",
  1249. " <td>53</td>\n",
  1250. " <td>4</td>\n",
  1251. " <td>13</td>\n",
  1252. " <td>1</td>\n",
  1253. " <td>...</td>\n",
  1254. " <td>3.0</td>\n",
  1255. " <td>0.0</td>\n",
  1256. " <td>1.0</td>\n",
  1257. " <td>3</td>\n",
  1258. " <td>11.0</td>\n",
  1259. " <td>1.0</td>\n",
  1260. " <td>1.0</td>\n",
  1261. " <td>1.0</td>\n",
  1262. " <td>0.0</td>\n",
  1263. " <td>0.0</td>\n",
  1264. " </tr>\n",
  1265. " <tr>\n",
  1266. " <th>99854</th>\n",
  1267. " <td>adamsau01</td>\n",
  1268. " <td>2015</td>\n",
  1269. " <td>1</td>\n",
  1270. " <td>CLE</td>\n",
  1271. " <td>AL</td>\n",
  1272. " <td>28</td>\n",
  1273. " <td>1</td>\n",
  1274. " <td>0</td>\n",
  1275. " <td>0</td>\n",
  1276. " <td>0</td>\n",
  1277. " <td>...</td>\n",
  1278. " <td>0.0</td>\n",
  1279. " <td>0.0</td>\n",
  1280. " <td>0.0</td>\n",
  1281. " <td>0</td>\n",
  1282. " <td>0.0</td>\n",
  1283. " <td>0.0</td>\n",
  1284. " <td>0.0</td>\n",
  1285. " <td>0.0</td>\n",
  1286. " <td>0.0</td>\n",
  1287. " <td>1.0</td>\n",
  1288. " </tr>\n",
  1289. " <tr>\n",
  1290. " <th>99855</th>\n",
  1291. " <td>adamsma01</td>\n",
  1292. " <td>2015</td>\n",
  1293. " <td>1</td>\n",
  1294. " <td>SLN</td>\n",
  1295. " <td>NL</td>\n",
  1296. " <td>60</td>\n",
  1297. " <td>175</td>\n",
  1298. " <td>14</td>\n",
  1299. " <td>42</td>\n",
  1300. " <td>9</td>\n",
  1301. " <td>...</td>\n",
  1302. " <td>24.0</td>\n",
  1303. " <td>1.0</td>\n",
  1304. " <td>0.0</td>\n",
  1305. " <td>10</td>\n",
  1306. " <td>41.0</td>\n",
  1307. " <td>1.0</td>\n",
  1308. " <td>0.0</td>\n",
  1309. " <td>0.0</td>\n",
  1310. " <td>1.0</td>\n",
  1311. " <td>1.0</td>\n",
  1312. " </tr>\n",
  1313. " <tr>\n",
  1314. " <th>99856</th>\n",
  1315. " <td>adcocna01</td>\n",
  1316. " <td>2015</td>\n",
  1317. " <td>1</td>\n",
  1318. " <td>CIN</td>\n",
  1319. " <td>NL</td>\n",
  1320. " <td>13</td>\n",
  1321. " <td>0</td>\n",
  1322. " <td>0</td>\n",
  1323. " <td>0</td>\n",
  1324. " <td>0</td>\n",
  1325. " <td>...</td>\n",
  1326. " <td>0.0</td>\n",
  1327. " <td>0.0</td>\n",
  1328. " <td>0.0</td>\n",
  1329. " <td>0</td>\n",
  1330. " <td>0.0</td>\n",
  1331. " <td>0.0</td>\n",
  1332. " <td>0.0</td>\n",
  1333. " <td>0.0</td>\n",
  1334. " <td>0.0</td>\n",
  1335. " <td>0.0</td>\n",
  1336. " </tr>\n",
  1337. " </tbody>\n",
  1338. "</table>\n",
  1339. "<p>10 rows × 22 columns</p>\n",
  1340. "</div>"
  1341. ],
  1342. "text/plain": [
  1343. " playerID yearID stint teamID lgID G AB R H 2B ... \\\n",
  1344. "99847 aardsda01 2015 1 ATL NL 33 1 0 0 0 ... \n",
  1345. "99848 abadfe01 2015 1 OAK AL 62 0 0 0 0 ... \n",
  1346. "99849 abreujo02 2015 1 CHA AL 154 613 88 178 34 ... \n",
  1347. "99850 achteaj01 2015 1 MIN AL 11 0 0 0 0 ... \n",
  1348. "99851 ackledu01 2015 1 SEA AL 85 186 22 40 8 ... \n",
  1349. "99852 ackledu01 2015 2 NYA AL 23 52 6 15 3 ... \n",
  1350. "99853 adamecr01 2015 1 COL NL 26 53 4 13 1 ... \n",
  1351. "99854 adamsau01 2015 1 CLE AL 28 1 0 0 0 ... \n",
  1352. "99855 adamsma01 2015 1 SLN NL 60 175 14 42 9 ... \n",
  1353. "99856 adcocna01 2015 1 CIN NL 13 0 0 0 0 ... \n",
  1354. "\n",
  1355. " RBI SB CS BB SO IBB HBP SH SF GIDP \n",
  1356. "99847 0.0 0.0 0.0 0 1.0 0.0 0.0 0.0 0.0 0.0 \n",
  1357. "99848 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  1358. "99849 101.0 0.0 0.0 39 140.0 11.0 15.0 0.0 1.0 16.0 \n",
  1359. "99850 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  1360. "99851 19.0 2.0 2.0 14 38.0 0.0 1.0 3.0 3.0 3.0 \n",
  1361. "99852 11.0 0.0 0.0 4 7.0 0.0 0.0 0.0 1.0 0.0 \n",
  1362. "99853 3.0 0.0 1.0 3 11.0 1.0 1.0 1.0 0.0 0.0 \n",
  1363. "99854 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 1.0 \n",
  1364. "99855 24.0 1.0 0.0 10 41.0 1.0 0.0 0.0 1.0 1.0 \n",
  1365. "99856 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  1366. "\n",
  1367. "[10 rows x 22 columns]"
  1368. ]
  1369. },
  1370. "execution_count": 7,
  1371. "metadata": {},
  1372. "output_type": "execute_result"
  1373. }
  1374. ],
  1375. "source": [
  1376. "df.loc[df.yearID == 2015][:10] # note we're restricting the return to just the first 10 values"
  1377. ]
  1378. },
  1379. {
  1380. "cell_type": "markdown",
  1381. "metadata": {},
  1382. "source": [
  1383. "Now what if we wanted the restrict this further by team. Say we wanted to see only the [Minesota Twins](https://www.mlb.com/twins) player data for 2015. That is\n",
  1384. "\n",
  1385. "```\n",
  1386. "df.yearID == 2015\n",
  1387. "AND\n",
  1388. "df.teamID == \"MIN\"\n",
  1389. "```\n",
  1390. "\n",
  1391. "We simply put these in parethesis and use the `&` operator."
  1392. ]
  1393. },
  1394. {
  1395. "cell_type": "code",
  1396. "execution_count": 8,
  1397. "metadata": {},
  1398. "outputs": [
  1399. {
  1400. "data": {
  1401. "text/html": [
  1402. "<div>\n",
  1403. "<style>\n",
  1404. " .dataframe thead tr:only-child th {\n",
  1405. " text-align: right;\n",
  1406. " }\n",
  1407. "\n",
  1408. " .dataframe thead th {\n",
  1409. " text-align: left;\n",
  1410. " }\n",
  1411. "\n",
  1412. " .dataframe tbody tr th {\n",
  1413. " vertical-align: top;\n",
  1414. " }\n",
  1415. "</style>\n",
  1416. "<table border=\"1\" class=\"dataframe\">\n",
  1417. " <thead>\n",
  1418. " <tr style=\"text-align: right;\">\n",
  1419. " <th></th>\n",
  1420. " <th>playerID</th>\n",
  1421. " <th>yearID</th>\n",
  1422. " <th>stint</th>\n",
  1423. " <th>teamID</th>\n",
  1424. " <th>lgID</th>\n",
  1425. " <th>G</th>\n",
  1426. " <th>AB</th>\n",
  1427. " <th>R</th>\n",
  1428. " <th>H</th>\n",
  1429. " <th>2B</th>\n",
  1430. " <th>...</th>\n",
  1431. " <th>RBI</th>\n",
  1432. " <th>SB</th>\n",
  1433. " <th>CS</th>\n",
  1434. " <th>BB</th>\n",
  1435. " <th>SO</th>\n",
  1436. " <th>IBB</th>\n",
  1437. " <th>HBP</th>\n",
  1438. " <th>SH</th>\n",
  1439. " <th>SF</th>\n",
  1440. " <th>GIDP</th>\n",
  1441. " </tr>\n",
  1442. " </thead>\n",
  1443. " <tbody>\n",
  1444. " <tr>\n",
  1445. " <th>99850</th>\n",
  1446. " <td>achteaj01</td>\n",
  1447. " <td>2015</td>\n",
  1448. " <td>1</td>\n",
  1449. " <td>MIN</td>\n",
  1450. " <td>AL</td>\n",
  1451. " <td>11</td>\n",
  1452. " <td>0</td>\n",
  1453. " <td>0</td>\n",
  1454. " <td>0</td>\n",
  1455. " <td>0</td>\n",
  1456. " <td>...</td>\n",
  1457. " <td>0.0</td>\n",
  1458. " <td>0.0</td>\n",
  1459. " <td>0.0</td>\n",
  1460. " <td>0</td>\n",
  1461. " <td>0.0</td>\n",
  1462. " <td>0.0</td>\n",
  1463. " <td>0.0</td>\n",
  1464. " <td>0.0</td>\n",
  1465. " <td>0.0</td>\n",
  1466. " <td>0.0</td>\n",
  1467. " </tr>\n",
  1468. " <tr>\n",
  1469. " <th>99891</th>\n",
  1470. " <td>arciaos01</td>\n",
  1471. " <td>2015</td>\n",
  1472. " <td>1</td>\n",
  1473. " <td>MIN</td>\n",
  1474. " <td>AL</td>\n",
  1475. " <td>19</td>\n",
  1476. " <td>58</td>\n",
  1477. " <td>6</td>\n",
  1478. " <td>16</td>\n",
  1479. " <td>0</td>\n",
  1480. " <td>...</td>\n",
  1481. " <td>8.0</td>\n",
  1482. " <td>0.0</td>\n",
  1483. " <td>0.0</td>\n",
  1484. " <td>4</td>\n",
  1485. " <td>15.0</td>\n",
  1486. " <td>4.0</td>\n",
  1487. " <td>2.0</td>\n",
  1488. " <td>0.0</td>\n",
  1489. " <td>1.0</td>\n",
  1490. " <td>2.0</td>\n",
  1491. " </tr>\n",
  1492. " <tr>\n",
  1493. " <th>99954</th>\n",
  1494. " <td>bernido01</td>\n",
  1495. " <td>2015</td>\n",
  1496. " <td>1</td>\n",
  1497. " <td>MIN</td>\n",
  1498. " <td>AL</td>\n",
  1499. " <td>4</td>\n",
  1500. " <td>5</td>\n",
  1501. " <td>1</td>\n",
  1502. " <td>1</td>\n",
  1503. " <td>1</td>\n",
  1504. " <td>...</td>\n",
  1505. " <td>2.0</td>\n",
  1506. " <td>0.0</td>\n",
  1507. " <td>0.0</td>\n",
  1508. " <td>1</td>\n",
  1509. " <td>3.0</td>\n",
  1510. " <td>0.0</td>\n",
  1511. " <td>0.0</td>\n",
  1512. " <td>0.0</td>\n",
  1513. " <td>0.0</td>\n",
  1514. " <td>0.0</td>\n",
  1515. " </tr>\n",
  1516. " <tr>\n",
  1517. " <th>99988</th>\n",
  1518. " <td>boyerbl01</td>\n",
  1519. " <td>2015</td>\n",
  1520. " <td>1</td>\n",
  1521. " <td>MIN</td>\n",
  1522. " <td>AL</td>\n",
  1523. " <td>68</td>\n",
  1524. " <td>0</td>\n",
  1525. " <td>0</td>\n",
  1526. " <td>0</td>\n",
  1527. " <td>0</td>\n",
  1528. " <td>...</td>\n",
  1529. " <td>0.0</td>\n",
  1530. " <td>0.0</td>\n",
  1531. " <td>0.0</td>\n",
  1532. " <td>0</td>\n",
  1533. " <td>0.0</td>\n",
  1534. " <td>0.0</td>\n",
  1535. " <td>0.0</td>\n",
  1536. " <td>0.0</td>\n",
  1537. " <td>0.0</td>\n",
  1538. " <td>0.0</td>\n",
  1539. " </tr>\n",
  1540. " <tr>\n",
  1541. " <th>100030</th>\n",
  1542. " <td>buxtoby01</td>\n",
  1543. " <td>2015</td>\n",
  1544. " <td>1</td>\n",
  1545. " <td>MIN</td>\n",
  1546. " <td>AL</td>\n",
  1547. " <td>46</td>\n",
  1548. " <td>129</td>\n",
  1549. " <td>16</td>\n",
  1550. " <td>27</td>\n",
  1551. " <td>7</td>\n",
  1552. " <td>...</td>\n",
  1553. " <td>6.0</td>\n",
  1554. " <td>2.0</td>\n",
  1555. " <td>2.0</td>\n",
  1556. " <td>6</td>\n",
  1557. " <td>44.0</td>\n",
  1558. " <td>0.0</td>\n",
  1559. " <td>1.0</td>\n",
  1560. " <td>2.0</td>\n",
  1561. " <td>0.0</td>\n",
  1562. " <td>1.0</td>\n",
  1563. " </tr>\n",
  1564. " <tr>\n",
  1565. " <th>100139</th>\n",
  1566. " <td>cottsne01</td>\n",
  1567. " <td>2015</td>\n",
  1568. " <td>2</td>\n",
  1569. " <td>MIN</td>\n",
  1570. " <td>AL</td>\n",
  1571. " <td>17</td>\n",
  1572. " <td>0</td>\n",
  1573. " <td>0</td>\n",
  1574. " <td>0</td>\n",
  1575. " <td>0</td>\n",
  1576. " <td>...</td>\n",
  1577. " <td>0.0</td>\n",
  1578. " <td>0.0</td>\n",
  1579. " <td>0.0</td>\n",
  1580. " <td>0</td>\n",
  1581. " <td>0.0</td>\n",
  1582. " <td>0.0</td>\n",
  1583. " <td>0.0</td>\n",
  1584. " <td>0.0</td>\n",
  1585. " <td>0.0</td>\n",
  1586. " <td>0.0</td>\n",
  1587. " </tr>\n",
  1588. " <tr>\n",
  1589. " <th>100215</th>\n",
  1590. " <td>doziebr01</td>\n",
  1591. " <td>2015</td>\n",
  1592. " <td>1</td>\n",
  1593. " <td>MIN</td>\n",
  1594. " <td>AL</td>\n",
  1595. " <td>157</td>\n",
  1596. " <td>628</td>\n",
  1597. " <td>101</td>\n",
  1598. " <td>148</td>\n",
  1599. " <td>39</td>\n",
  1600. " <td>...</td>\n",
  1601. " <td>77.0</td>\n",
  1602. " <td>12.0</td>\n",
  1603. " <td>4.0</td>\n",
  1604. " <td>61</td>\n",
  1605. " <td>148.0</td>\n",
  1606. " <td>2.0</td>\n",
  1607. " <td>7.0</td>\n",
  1608. " <td>0.0</td>\n",
  1609. " <td>8.0</td>\n",
  1610. " <td>10.0</td>\n",
  1611. " </tr>\n",
  1612. " <tr>\n",
  1613. " <th>100221</th>\n",
  1614. " <td>duensbr01</td>\n",
  1615. " <td>2015</td>\n",
  1616. " <td>1</td>\n",
  1617. " <td>MIN</td>\n",
  1618. " <td>AL</td>\n",
  1619. " <td>55</td>\n",
  1620. " <td>1</td>\n",
  1621. " <td>0</td>\n",
  1622. " <td>0</td>\n",
  1623. " <td>0</td>\n",
  1624. " <td>...</td>\n",
  1625. " <td>0.0</td>\n",
  1626. " <td>0.0</td>\n",
  1627. " <td>0.0</td>\n",
  1628. " <td>0</td>\n",
  1629. " <td>0.0</td>\n",
  1630. " <td>0.0</td>\n",
  1631. " <td>0.0</td>\n",
  1632. " <td>0.0</td>\n",
  1633. " <td>0.0</td>\n",
  1634. " <td>0.0</td>\n",
  1635. " </tr>\n",
  1636. " <tr>\n",
  1637. " <th>100222</th>\n",
  1638. " <td>duffety01</td>\n",
  1639. " <td>2015</td>\n",
  1640. " <td>1</td>\n",
  1641. " <td>MIN</td>\n",
  1642. " <td>AL</td>\n",
  1643. " <td>10</td>\n",
  1644. " <td>0</td>\n",
  1645. " <td>0</td>\n",
  1646. " <td>0</td>\n",
  1647. " <td>0</td>\n",
  1648. " <td>...</td>\n",
  1649. " <td>0.0</td>\n",
  1650. " <td>0.0</td>\n",
  1651. " <td>0.0</td>\n",
  1652. " <td>0</td>\n",
  1653. " <td>0.0</td>\n",
  1654. " <td>0.0</td>\n",
  1655. " <td>0.0</td>\n",
  1656. " <td>0.0</td>\n",
  1657. " <td>0.0</td>\n",
  1658. " <td>0.0</td>\n",
  1659. " </tr>\n",
  1660. " <tr>\n",
  1661. " <th>100249</th>\n",
  1662. " <td>escobed01</td>\n",
  1663. " <td>2015</td>\n",
  1664. " <td>1</td>\n",
  1665. " <td>MIN</td>\n",
  1666. " <td>AL</td>\n",
  1667. " <td>127</td>\n",
  1668. " <td>409</td>\n",
  1669. " <td>48</td>\n",
  1670. " <td>107</td>\n",
  1671. " <td>31</td>\n",
  1672. " <td>...</td>\n",
  1673. " <td>58.0</td>\n",
  1674. " <td>2.0</td>\n",
  1675. " <td>3.0</td>\n",
  1676. " <td>28</td>\n",
  1677. " <td>86.0</td>\n",
  1678. " <td>1.0</td>\n",
  1679. " <td>2.0</td>\n",
  1680. " <td>2.0</td>\n",
  1681. " <td>5.0</td>\n",
  1682. " <td>7.0</td>\n",
  1683. " </tr>\n",
  1684. " </tbody>\n",
  1685. "</table>\n",
  1686. "<p>10 rows × 22 columns</p>\n",
  1687. "</div>"
  1688. ],
  1689. "text/plain": [
  1690. " playerID yearID stint teamID lgID G AB R H 2B ... \\\n",
  1691. "99850 achteaj01 2015 1 MIN AL 11 0 0 0 0 ... \n",
  1692. "99891 arciaos01 2015 1 MIN AL 19 58 6 16 0 ... \n",
  1693. "99954 bernido01 2015 1 MIN AL 4 5 1 1 1 ... \n",
  1694. "99988 boyerbl01 2015 1 MIN AL 68 0 0 0 0 ... \n",
  1695. "100030 buxtoby01 2015 1 MIN AL 46 129 16 27 7 ... \n",
  1696. "100139 cottsne01 2015 2 MIN AL 17 0 0 0 0 ... \n",
  1697. "100215 doziebr01 2015 1 MIN AL 157 628 101 148 39 ... \n",
  1698. "100221 duensbr01 2015 1 MIN AL 55 1 0 0 0 ... \n",
  1699. "100222 duffety01 2015 1 MIN AL 10 0 0 0 0 ... \n",
  1700. "100249 escobed01 2015 1 MIN AL 127 409 48 107 31 ... \n",
  1701. "\n",
  1702. " RBI SB CS BB SO IBB HBP SH SF GIDP \n",
  1703. "99850 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  1704. "99891 8.0 0.0 0.0 4 15.0 4.0 2.0 0.0 1.0 2.0 \n",
  1705. "99954 2.0 0.0 0.0 1 3.0 0.0 0.0 0.0 0.0 0.0 \n",
  1706. "99988 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  1707. "100030 6.0 2.0 2.0 6 44.0 0.0 1.0 2.0 0.0 1.0 \n",
  1708. "100139 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  1709. "100215 77.0 12.0 4.0 61 148.0 2.0 7.0 0.0 8.0 10.0 \n",
  1710. "100221 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  1711. "100222 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
  1712. "100249 58.0 2.0 3.0 28 86.0 1.0 2.0 2.0 5.0 7.0 \n",
  1713. "\n",
  1714. "[10 rows x 22 columns]"
  1715. ]
  1716. },
  1717. "execution_count": 8,
  1718. "metadata": {},
  1719. "output_type": "execute_result"
  1720. }
  1721. ],
  1722. "source": [
  1723. "df.loc[(df.yearID == 2015) & (df.teamID == \"MIN\")].head(10)"
  1724. ]
  1725. },
  1726. {
  1727. "cell_type": "markdown",
  1728. "metadata": {},
  1729. "source": [
  1730. "Now what if we wanted to restrict a subset of columns. This is easy with `iloc[]` ... we will just use our boolean expression as above for the _row selection_ and then the list of columns for our _column selection_ (in this case a much smaller subset of data)."
  1731. ]
  1732. },
  1733. {
  1734. "cell_type": "code",
  1735. "execution_count": 9,
  1736. "metadata": {},
  1737. "outputs": [
  1738. {
  1739. "data": {
  1740. "text/html": [
  1741. "<div>\n",
  1742. "<style>\n",
  1743. " .dataframe thead tr:only-child th {\n",
  1744. " text-align: right;\n",
  1745. " }\n",
  1746. "\n",
  1747. " .dataframe thead th {\n",
  1748. " text-align: left;\n",
  1749. " }\n",
  1750. "\n",
  1751. " .dataframe tbody tr th {\n",
  1752. " vertical-align: top;\n",
  1753. " }\n",
  1754. "</style>\n",
  1755. "<table border=\"1\" class=\"dataframe\">\n",
  1756. " <thead>\n",
  1757. " <tr style=\"text-align: right;\">\n",
  1758. " <th></th>\n",
  1759. " <th>playerID</th>\n",
  1760. " <th>G</th>\n",
  1761. " <th>AB</th>\n",
  1762. " <th>H</th>\n",
  1763. " <th>HR</th>\n",
  1764. " <th>RBI</th>\n",
  1765. " </tr>\n",
  1766. " </thead>\n",
  1767. " <tbody>\n",
  1768. " <tr>\n",
  1769. " <th>99850</th>\n",
  1770. " <td>achteaj01</td>\n",
  1771. " <td>11</td>\n",
  1772. " <td>0</td>\n",
  1773. " <td>0</td>\n",
  1774. " <td>0</td>\n",
  1775. " <td>0.0</td>\n",
  1776. " </tr>\n",
  1777. " <tr>\n",
  1778. " <th>99891</th>\n",
  1779. " <td>arciaos01</td>\n",
  1780. " <td>19</td>\n",
  1781. " <td>58</td>\n",
  1782. " <td>16</td>\n",
  1783. " <td>2</td>\n",
  1784. " <td>8.0</td>\n",
  1785. " </tr>\n",
  1786. " <tr>\n",
  1787. " <th>99954</th>\n",
  1788. " <td>bernido01</td>\n",
  1789. " <td>4</td>\n",
  1790. " <td>5</td>\n",
  1791. " <td>1</td>\n",
  1792. " <td>0</td>\n",
  1793. " <td>2.0</td>\n",
  1794. " </tr>\n",
  1795. " <tr>\n",
  1796. " <th>99988</th>\n",
  1797. " <td>boyerbl01</td>\n",
  1798. " <td>68</td>\n",
  1799. " <td>0</td>\n",
  1800. " <td>0</td>\n",
  1801. " <td>0</td>\n",
  1802. " <td>0.0</td>\n",
  1803. " </tr>\n",
  1804. " <tr>\n",
  1805. " <th>100030</th>\n",
  1806. " <td>buxtoby01</td>\n",
  1807. " <td>46</td>\n",
  1808. " <td>129</td>\n",
  1809. " <td>27</td>\n",
  1810. " <td>2</td>\n",
  1811. " <td>6.0</td>\n",
  1812. " </tr>\n",
  1813. " <tr>\n",
  1814. " <th>100139</th>\n",
  1815. " <td>cottsne01</td>\n",
  1816. " <td>17</td>\n",
  1817. " <td>0</td>\n",
  1818. " <td>0</td>\n",
  1819. " <td>0</td>\n",
  1820. " <td>0.0</td>\n",
  1821. " </tr>\n",
  1822. " <tr>\n",
  1823. " <th>100215</th>\n",
  1824. " <td>doziebr01</td>\n",
  1825. " <td>157</td>\n",
  1826. " <td>628</td>\n",
  1827. " <td>148</td>\n",
  1828. " <td>28</td>\n",
  1829. " <td>77.0</td>\n",
  1830. " </tr>\n",
  1831. " <tr>\n",
  1832. " <th>100221</th>\n",
  1833. " <td>duensbr01</td>\n",
  1834. " <td>55</td>\n",
  1835. " <td>1</td>\n",
  1836. " <td>0</td>\n",
  1837. " <td>0</td>\n",
  1838. " <td>0.0</td>\n",
  1839. " </tr>\n",
  1840. " <tr>\n",
  1841. " <th>100222</th>\n",
  1842. " <td>duffety01</td>\n",
  1843. " <td>10</td>\n",
  1844. " <td>0</td>\n",
  1845. " <td>0</td>\n",
  1846. " <td>0</td>\n",
  1847. " <td>0.0</td>\n",
  1848. " </tr>\n",
  1849. " <tr>\n",
  1850. " <th>100249</th>\n",
  1851. " <td>escobed01</td>\n",
  1852. " <td>127</td>\n",
  1853. " <td>409</td>\n",
  1854. " <td>107</td>\n",
  1855. " <td>12</td>\n",
  1856. " <td>58.0</td>\n",
  1857. " </tr>\n",
  1858. " <tr>\n",
  1859. " <th>100270</th>\n",
  1860. " <td>fienca01</td>\n",
  1861. " <td>62</td>\n",
  1862. " <td>0</td>\n",
  1863. " <td>0</td>\n",
  1864. " <td>0</td>\n",
  1865. " <td>0.0</td>\n",
  1866. " </tr>\n",
  1867. " <tr>\n",
  1868. " <th>100302</th>\n",
  1869. " <td>fryerer01</td>\n",
  1870. " <td>15</td>\n",
  1871. " <td>22</td>\n",
  1872. " <td>5</td>\n",
  1873. " <td>0</td>\n",
  1874. " <td>2.0</td>\n",
  1875. " </tr>\n",
  1876. " <tr>\n",
  1877. " <th>100333</th>\n",
  1878. " <td>gibsoky01</td>\n",
  1879. " <td>32</td>\n",
  1880. " <td>5</td>\n",
  1881. " <td>1</td>\n",
  1882. " <td>0</td>\n",
  1883. " <td>0.0</td>\n",
  1884. " </tr>\n",
  1885. " <tr>\n",
  1886. " <th>100373</th>\n",
  1887. " <td>grahajr01</td>\n",
  1888. " <td>39</td>\n",
  1889. " <td>0</td>\n",
  1890. " <td>0</td>\n",
  1891. " <td>0</td>\n",
  1892. " <td>0.0</td>\n",
  1893. " </tr>\n",
  1894. " <tr>\n",
  1895. " <th>100455</th>\n",
  1896. " <td>herrmch01</td>\n",
  1897. " <td>45</td>\n",
  1898. " <td>103</td>\n",
  1899. " <td>15</td>\n",
  1900. " <td>2</td>\n",
  1901. " <td>10.0</td>\n",
  1902. " </tr>\n",
  1903. " <tr>\n",
  1904. " <th>100459</th>\n",
  1905. " <td>hicksaa01</td>\n",
  1906. " <td>97</td>\n",
  1907. " <td>352</td>\n",
  1908. " <td>90</td>\n",
  1909. " <td>11</td>\n",
  1910. " <td>33.0</td>\n",
  1911. " </tr>\n",
  1912. " <tr>\n",
  1913. " <th>100486</th>\n",
  1914. " <td>hugheph01</td>\n",
  1915. " <td>27</td>\n",
  1916. " <td>3</td>\n",
  1917. " <td>0</td>\n",
  1918. " <td>0</td>\n",
  1919. " <td>0.0</td>\n",
  1920. " </tr>\n",
  1921. " <tr>\n",
  1922. " <th>100488</th>\n",
  1923. " <td>hunteto01</td>\n",
  1924. " <td>139</td>\n",
  1925. " <td>521</td>\n",
  1926. " <td>125</td>\n",
  1927. " <td>22</td>\n",
  1928. " <td>81.0</td>\n",
  1929. " </tr>\n",
  1930. " <tr>\n",
  1931. " <th>100521</th>\n",
  1932. " <td>jepseke01</td>\n",
  1933. " <td>29</td>\n",
  1934. " <td>0</td>\n",
  1935. " <td>0</td>\n",
  1936. " <td>0</td>\n",
  1937. " <td>0.0</td>\n",
  1938. " </tr>\n",
  1939. " <tr>\n",
  1940. " <th>100564</th>\n",
  1941. " <td>keplema01</td>\n",
  1942. " <td>3</td>\n",
  1943. " <td>7</td>\n",
  1944. " <td>1</td>\n",
  1945. " <td>0</td>\n",
  1946. " <td>0.0</td>\n",
  1947. " </tr>\n",
  1948. " <tr>\n",
  1949. " <th>100696</th>\n",
  1950. " <td>mauerjo01</td>\n",
  1951. " <td>158</td>\n",
  1952. " <td>592</td>\n",
  1953. " <td>157</td>\n",
  1954. " <td>10</td>\n",
  1955. " <td>66.0</td>\n",
  1956. " </tr>\n",
  1957. " <tr>\n",
  1958. " <th>100701</th>\n",
  1959. " <td>maytr01</td>\n",
  1960. " <td>48</td>\n",
  1961. " <td>3</td>\n",
  1962. " <td>0</td>\n",
  1963. " <td>0</td>\n",
  1964. " <td>0.0</td>\n",
  1965. " </tr>\n",
  1966. " <tr>\n",
  1967. " <th>100729</th>\n",
  1968. " <td>meyeral01</td>\n",
  1969. " <td>2</td>\n",
  1970. " <td>0</td>\n",
  1971. " <td>0</td>\n",
  1972. " <td>0</td>\n",
  1973. " <td>0.0</td>\n",
  1974. " </tr>\n",
  1975. " <tr>\n",
  1976. " <th>100737</th>\n",
  1977. " <td>milonto01</td>\n",
  1978. " <td>24</td>\n",
  1979. " <td>2</td>\n",
  1980. " <td>0</td>\n",
  1981. " <td>0</td>\n",
  1982. " <td>0.0</td>\n",
  1983. " </tr>\n",
  1984. " <tr>\n",
  1985. " <th>100807</th>\n",
  1986. " <td>nolasri01</td>\n",
  1987. " <td>9</td>\n",
  1988. " <td>3</td>\n",
  1989. " <td>0</td>\n",
  1990. " <td>0</td>\n",
  1991. " <td>0.0</td>\n",
  1992. " </tr>\n",
  1993. " <tr>\n",
  1994. " <th>100816</th>\n",
  1995. " <td>nunezed02</td>\n",
  1996. " <td>72</td>\n",
  1997. " <td>188</td>\n",
  1998. " <td>53</td>\n",
  1999. " <td>4</td>\n",
  2000. " <td>20.0</td>\n",
  2001. " </tr>\n",
  2002. " <tr>\n",
  2003. " <th>100837</th>\n",
  2004. " <td>orourry01</td>\n",
  2005. " <td>28</td>\n",
  2006. " <td>0</td>\n",
  2007. " <td>0</td>\n",
  2008. " <td>0</td>\n",
  2009. " <td>0.0</td>\n",
  2010. " </tr>\n",
  2011. " <tr>\n",
  2012. " <th>100872</th>\n",
  2013. " <td>pelfrmi01</td>\n",
  2014. " <td>30</td>\n",
  2015. " <td>3</td>\n",
  2016. " <td>2</td>\n",
  2017. " <td>0</td>\n",
  2018. " <td>0.0</td>\n",
  2019. " </tr>\n",
  2020. " <tr>\n",
  2021. " <th>100895</th>\n",
  2022. " <td>perkigl01</td>\n",
  2023. " <td>60</td>\n",
  2024. " <td>0</td>\n",
  2025. " <td>0</td>\n",
  2026. " <td>0</td>\n",
  2027. " <td>0.0</td>\n",
  2028. " </tr>\n",
  2029. " <tr>\n",
  2030. " <th>100915</th>\n",
  2031. " <td>plouftr01</td>\n",
  2032. " <td>152</td>\n",
  2033. " <td>573</td>\n",
  2034. " <td>140</td>\n",
  2035. " <td>22</td>\n",
  2036. " <td>86.0</td>\n",
  2037. " </tr>\n",
  2038. " <tr>\n",
  2039. " <th>100917</th>\n",
  2040. " <td>polanjo01</td>\n",
  2041. " <td>4</td>\n",
  2042. " <td>10</td>\n",
  2043. " <td>3</td>\n",
  2044. " <td>0</td>\n",
  2045. " <td>1.0</td>\n",
  2046. " </tr>\n",
  2047. " <tr>\n",
  2048. " <th>100925</th>\n",
  2049. " <td>pressry01</td>\n",
  2050. " <td>27</td>\n",
  2051. " <td>0</td>\n",
  2052. " <td>0</td>\n",
  2053. " <td>0</td>\n",
  2054. " <td>0.0</td>\n",
  2055. " </tr>\n",
  2056. " <tr>\n",
  2057. " <th>100994</th>\n",
  2058. " <td>robinsh01</td>\n",
  2059. " <td>83</td>\n",
  2060. " <td>180</td>\n",
  2061. " <td>45</td>\n",
  2062. " <td>0</td>\n",
  2063. " <td>16.0</td>\n",
  2064. " </tr>\n",
  2065. " <tr>\n",
  2066. " <th>101023</th>\n",
  2067. " <td>rosared01</td>\n",
  2068. " <td>122</td>\n",
  2069. " <td>453</td>\n",
  2070. " <td>121</td>\n",
  2071. " <td>13</td>\n",
  2072. " <td>50.0</td>\n",
  2073. " </tr>\n",
  2074. " <tr>\n",
  2075. " <th>101067</th>\n",
  2076. " <td>sanomi01</td>\n",
  2077. " <td>80</td>\n",
  2078. " <td>279</td>\n",
  2079. " <td>75</td>\n",
  2080. " <td>18</td>\n",
  2081. " <td>52.0</td>\n",
  2082. " </tr>\n",
  2083. " <tr>\n",
  2084. " <th>101069</th>\n",
  2085. " <td>santada01</td>\n",
  2086. " <td>91</td>\n",
  2087. " <td>261</td>\n",
  2088. " <td>56</td>\n",
  2089. " <td>0</td>\n",
  2090. " <td>21.0</td>\n",
  2091. " </tr>\n",
  2092. " <tr>\n",
  2093. " <th>101072</th>\n",
  2094. " <td>santaer01</td>\n",
  2095. " <td>17</td>\n",
  2096. " <td>0</td>\n",
  2097. " <td>0</td>\n",
  2098. " <td>0</td>\n",
  2099. " <td>0.0</td>\n",
  2100. " </tr>\n",
  2101. " <tr>\n",
  2102. " <th>101079</th>\n",
  2103. " <td>schafjo02</td>\n",
  2104. " <td>27</td>\n",
  2105. " <td>69</td>\n",
  2106. " <td>15</td>\n",
  2107. " <td>0</td>\n",
  2108. " <td>5.0</td>\n",
  2109. " </tr>\n",
  2110. " <tr>\n",
  2111. " <th>101144</th>\n",
  2112. " <td>staufti01</td>\n",
  2113. " <td>13</td>\n",
  2114. " <td>0</td>\n",
  2115. " <td>0</td>\n",
  2116. " <td>0</td>\n",
  2117. " <td>0.0</td>\n",
  2118. " </tr>\n",
  2119. " <tr>\n",
  2120. " <th>101164</th>\n",
  2121. " <td>suzukku01</td>\n",
  2122. " <td>131</td>\n",
  2123. " <td>433</td>\n",
  2124. " <td>104</td>\n",
  2125. " <td>5</td>\n",
  2126. " <td>50.0</td>\n",
  2127. " </tr>\n",
  2128. " <tr>\n",
  2129. " <th>101189</th>\n",
  2130. " <td>thielca01</td>\n",
  2131. " <td>6</td>\n",
  2132. " <td>0</td>\n",
  2133. " <td>0</td>\n",
  2134. " <td>0</td>\n",
  2135. " <td>0.0</td>\n",
  2136. " </tr>\n",
  2137. " <tr>\n",
  2138. " <th>101193</th>\n",
  2139. " <td>thompaa01</td>\n",
  2140. " <td>41</td>\n",
  2141. " <td>0</td>\n",
  2142. " <td>0</td>\n",
  2143. " <td>0</td>\n",
  2144. " <td>0.0</td>\n",
  2145. " </tr>\n",
  2146. " <tr>\n",
  2147. " <th>101203</th>\n",
  2148. " <td>tonkimi01</td>\n",
  2149. " <td>26</td>\n",
  2150. " <td>0</td>\n",
  2151. " <td>0</td>\n",
  2152. " <td>0</td>\n",
  2153. " <td>0.0</td>\n",
  2154. " </tr>\n",
  2155. " <tr>\n",
  2156. " <th>101240</th>\n",
  2157. " <td>vargake01</td>\n",
  2158. " <td>58</td>\n",
  2159. " <td>175</td>\n",
  2160. " <td>42</td>\n",
  2161. " <td>5</td>\n",
  2162. " <td>17.0</td>\n",
  2163. " </tr>\n",
  2164. " </tbody>\n",
  2165. "</table>\n",
  2166. "</div>"
  2167. ],
  2168. "text/plain": [
  2169. " playerID G AB H HR RBI\n",
  2170. "99850 achteaj01 11 0 0 0 0.0\n",
  2171. "99891 arciaos01 19 58 16 2 8.0\n",
  2172. "99954 bernido01 4 5 1 0 2.0\n",
  2173. "99988 boyerbl01 68 0 0 0 0.0\n",
  2174. "100030 buxtoby01 46 129 27 2 6.0\n",
  2175. "100139 cottsne01 17 0 0 0 0.0\n",
  2176. "100215 doziebr01 157 628 148 28 77.0\n",
  2177. "100221 duensbr01 55 1 0 0 0.0\n",
  2178. "100222 duffety01 10 0 0 0 0.0\n",
  2179. "100249 escobed01 127 409 107 12 58.0\n",
  2180. "100270 fienca01 62 0 0 0 0.0\n",
  2181. "100302 fryerer01 15 22 5 0 2.0\n",
  2182. "100333 gibsoky01 32 5 1 0 0.0\n",
  2183. "100373 grahajr01 39 0 0 0 0.0\n",
  2184. "100455 herrmch01 45 103 15 2 10.0\n",
  2185. "100459 hicksaa01 97 352 90 11 33.0\n",
  2186. "100486 hugheph01 27 3 0 0 0.0\n",
  2187. "100488 hunteto01 139 521 125 22 81.0\n",
  2188. "100521 jepseke01 29 0 0 0 0.0\n",
  2189. "100564 keplema01 3 7 1 0 0.0\n",
  2190. "100696 mauerjo01 158 592 157 10 66.0\n",
  2191. "100701 maytr01 48 3 0 0 0.0\n",
  2192. "100729 meyeral01 2 0 0 0 0.0\n",
  2193. "100737 milonto01 24 2 0 0 0.0\n",
  2194. "100807 nolasri01 9 3 0 0 0.0\n",
  2195. "100816 nunezed02 72 188 53 4 20.0\n",
  2196. "100837 orourry01 28 0 0 0 0.0\n",
  2197. "100872 pelfrmi01 30 3 2 0 0.0\n",
  2198. "100895 perkigl01 60 0 0 0 0.0\n",
  2199. "100915 plouftr01 152 573 140 22 86.0\n",
  2200. "100917 polanjo01 4 10 3 0 1.0\n",
  2201. "100925 pressry01 27 0 0 0 0.0\n",
  2202. "100994 robinsh01 83 180 45 0 16.0\n",
  2203. "101023 rosared01 122 453 121 13 50.0\n",
  2204. "101067 sanomi01 80 279 75 18 52.0\n",
  2205. "101069 santada01 91 261 56 0 21.0\n",
  2206. "101072 santaer01 17 0 0 0 0.0\n",
  2207. "101079 schafjo02 27 69 15 0 5.0\n",
  2208. "101144 staufti01 13 0 0 0 0.0\n",
  2209. "101164 suzukku01 131 433 104 5 50.0\n",
  2210. "101189 thielca01 6 0 0 0 0.0\n",
  2211. "101193 thompaa01 41 0 0 0 0.0\n",
  2212. "101203 tonkimi01 26 0 0 0 0.0\n",
  2213. "101240 vargake01 58 175 42 5 17.0"
  2214. ]
  2215. },
  2216. "execution_count": 9,
  2217. "metadata": {},
  2218. "output_type": "execute_result"
  2219. }
  2220. ],
  2221. "source": [
  2222. "df.loc[(df.yearID == 2015) & (df.teamID == \"MIN\"),\\\n",
  2223. " ['playerID', 'G', 'AB', 'H', 'HR', 'RBI']]"
  2224. ]
  2225. },
  2226. {
  2227. "cell_type": "markdown",
  2228. "metadata": {},
  2229. "source": [
  2230. "## Sorting"
  2231. ]
  2232. },
  2233. {
  2234. "cell_type": "markdown",
  2235. "metadata": {},
  2236. "source": [
  2237. "Sorting is facilitated by the [`sort_values()` method](). By default, sorting is done in _ascending order_, specify the parameter `ascending=False` to get descending order."
  2238. ]
  2239. },
  2240. {
  2241. "cell_type": "code",
  2242. "execution_count": 10,
  2243. "metadata": {},
  2244. "outputs": [
  2245. {
  2246. "data": {
  2247. "text/html": [
  2248. "<div>\n",
  2249. "<style>\n",
  2250. " .dataframe thead tr:only-child th {\n",
  2251. " text-align: right;\n",
  2252. " }\n",
  2253. "\n",
  2254. " .dataframe thead th {\n",
  2255. " text-align: left;\n",
  2256. " }\n",
  2257. "\n",
  2258. " .dataframe tbody tr th {\n",
  2259. " vertical-align: top;\n",
  2260. " }\n",
  2261. "</style>\n",
  2262. "<table border=\"1\" class=\"dataframe\">\n",
  2263. " <thead>\n",
  2264. " <tr style=\"text-align: right;\">\n",
  2265. " <th></th>\n",
  2266. " <th>playerID</th>\n",
  2267. " <th>G</th>\n",
  2268. " <th>AB</th>\n",
  2269. " <th>H</th>\n",
  2270. " <th>HR</th>\n",
  2271. " <th>RBI</th>\n",
  2272. " </tr>\n",
  2273. " </thead>\n",
  2274. " <tbody>\n",
  2275. " <tr>\n",
  2276. " <th>100696</th>\n",
  2277. " <td>mauerjo01</td>\n",
  2278. " <td>158</td>\n",
  2279. " <td>592</td>\n",
  2280. " <td>157</td>\n",
  2281. " <td>10</td>\n",
  2282. " <td>66.0</td>\n",
  2283. " </tr>\n",
  2284. " <tr>\n",
  2285. " <th>100215</th>\n",
  2286. " <td>doziebr01</td>\n",
  2287. " <td>157</td>\n",
  2288. " <td>628</td>\n",
  2289. " <td>148</td>\n",
  2290. " <td>28</td>\n",
  2291. " <td>77.0</td>\n",
  2292. " </tr>\n",
  2293. " <tr>\n",
  2294. " <th>100915</th>\n",
  2295. " <td>plouftr01</td>\n",
  2296. " <td>152</td>\n",
  2297. " <td>573</td>\n",
  2298. " <td>140</td>\n",
  2299. " <td>22</td>\n",
  2300. " <td>86.0</td>\n",
  2301. " </tr>\n",
  2302. " <tr>\n",
  2303. " <th>100488</th>\n",
  2304. " <td>hunteto01</td>\n",
  2305. " <td>139</td>\n",
  2306. " <td>521</td>\n",
  2307. " <td>125</td>\n",
  2308. " <td>22</td>\n",
  2309. " <td>81.0</td>\n",
  2310. " </tr>\n",
  2311. " <tr>\n",
  2312. " <th>101164</th>\n",
  2313. " <td>suzukku01</td>\n",
  2314. " <td>131</td>\n",
  2315. " <td>433</td>\n",
  2316. " <td>104</td>\n",
  2317. " <td>5</td>\n",
  2318. " <td>50.0</td>\n",
  2319. " </tr>\n",
  2320. " <tr>\n",
  2321. " <th>100249</th>\n",
  2322. " <td>escobed01</td>\n",
  2323. " <td>127</td>\n",
  2324. " <td>409</td>\n",
  2325. " <td>107</td>\n",
  2326. " <td>12</td>\n",
  2327. " <td>58.0</td>\n",
  2328. " </tr>\n",
  2329. " <tr>\n",
  2330. " <th>101023</th>\n",
  2331. " <td>rosared01</td>\n",
  2332. " <td>122</td>\n",
  2333. " <td>453</td>\n",
  2334. " <td>121</td>\n",
  2335. " <td>13</td>\n",
  2336. " <td>50.0</td>\n",
  2337. " </tr>\n",
  2338. " <tr>\n",
  2339. " <th>100459</th>\n",
  2340. " <td>hicksaa01</td>\n",
  2341. " <td>97</td>\n",
  2342. " <td>352</td>\n",
  2343. " <td>90</td>\n",
  2344. " <td>11</td>\n",
  2345. " <td>33.0</td>\n",
  2346. " </tr>\n",
  2347. " <tr>\n",
  2348. " <th>101069</th>\n",
  2349. " <td>santada01</td>\n",
  2350. " <td>91</td>\n",
  2351. " <td>261</td>\n",
  2352. " <td>56</td>\n",
  2353. " <td>0</td>\n",
  2354. " <td>21.0</td>\n",
  2355. " </tr>\n",
  2356. " <tr>\n",
  2357. " <th>100994</th>\n",
  2358. " <td>robinsh01</td>\n",
  2359. " <td>83</td>\n",
  2360. " <td>180</td>\n",
  2361. " <td>45</td>\n",
  2362. " <td>0</td>\n",
  2363. " <td>16.0</td>\n",
  2364. " </tr>\n",
  2365. " <tr>\n",
  2366. " <th>101067</th>\n",
  2367. " <td>sanomi01</td>\n",
  2368. " <td>80</td>\n",
  2369. " <td>279</td>\n",
  2370. " <td>75</td>\n",
  2371. " <td>18</td>\n",
  2372. " <td>52.0</td>\n",
  2373. " </tr>\n",
  2374. " <tr>\n",
  2375. " <th>100816</th>\n",
  2376. " <td>nunezed02</td>\n",
  2377. " <td>72</td>\n",
  2378. " <td>188</td>\n",
  2379. " <td>53</td>\n",
  2380. " <td>4</td>\n",
  2381. " <td>20.0</td>\n",
  2382. " </tr>\n",
  2383. " <tr>\n",
  2384. " <th>99988</th>\n",
  2385. " <td>boyerbl01</td>\n",
  2386. " <td>68</td>\n",
  2387. " <td>0</td>\n",
  2388. " <td>0</td>\n",
  2389. " <td>0</td>\n",
  2390. " <td>0.0</td>\n",
  2391. " </tr>\n",
  2392. " <tr>\n",
  2393. " <th>100270</th>\n",
  2394. " <td>fienca01</td>\n",
  2395. " <td>62</td>\n",
  2396. " <td>0</td>\n",
  2397. " <td>0</td>\n",
  2398. " <td>0</td>\n",
  2399. " <td>0.0</td>\n",
  2400. " </tr>\n",
  2401. " <tr>\n",
  2402. " <th>100895</th>\n",
  2403. " <td>perkigl01</td>\n",
  2404. " <td>60</td>\n",
  2405. " <td>0</td>\n",
  2406. " <td>0</td>\n",
  2407. " <td>0</td>\n",
  2408. " <td>0.0</td>\n",
  2409. " </tr>\n",
  2410. " <tr>\n",
  2411. " <th>101240</th>\n",
  2412. " <td>vargake01</td>\n",
  2413. " <td>58</td>\n",
  2414. " <td>175</td>\n",
  2415. " <td>42</td>\n",
  2416. " <td>5</td>\n",
  2417. " <td>17.0</td>\n",
  2418. " </tr>\n",
  2419. " <tr>\n",
  2420. " <th>100221</th>\n",
  2421. " <td>duensbr01</td>\n",
  2422. " <td>55</td>\n",
  2423. " <td>1</td>\n",
  2424. " <td>0</td>\n",
  2425. " <td>0</td>\n",
  2426. " <td>0.0</td>\n",
  2427. " </tr>\n",
  2428. " <tr>\n",
  2429. " <th>100701</th>\n",
  2430. " <td>maytr01</td>\n",
  2431. " <td>48</td>\n",
  2432. " <td>3</td>\n",
  2433. " <td>0</td>\n",
  2434. " <td>0</td>\n",
  2435. " <td>0.0</td>\n",
  2436. " </tr>\n",
  2437. " <tr>\n",
  2438. " <th>100030</th>\n",
  2439. " <td>buxtoby01</td>\n",
  2440. " <td>46</td>\n",
  2441. " <td>129</td>\n",
  2442. " <td>27</td>\n",
  2443. " <td>2</td>\n",
  2444. " <td>6.0</td>\n",
  2445. " </tr>\n",
  2446. " <tr>\n",
  2447. " <th>100455</th>\n",
  2448. " <td>herrmch01</td>\n",
  2449. " <td>45</td>\n",
  2450. " <td>103</td>\n",
  2451. " <td>15</td>\n",
  2452. " <td>2</td>\n",
  2453. " <td>10.0</td>\n",
  2454. " </tr>\n",
  2455. " </tbody>\n",
  2456. "</table>\n",
  2457. "</div>"
  2458. ],
  2459. "text/plain": [
  2460. " playerID G AB H HR RBI\n",
  2461. "100696 mauerjo01 158 592 157 10 66.0\n",
  2462. "100215 doziebr01 157 628 148 28 77.0\n",
  2463. "100915 plouftr01 152 573 140 22 86.0\n",
  2464. "100488 hunteto01 139 521 125 22 81.0\n",
  2465. "101164 suzukku01 131 433 104 5 50.0\n",
  2466. "100249 escobed01 127 409 107 12 58.0\n",
  2467. "101023 rosared01 122 453 121 13 50.0\n",
  2468. "100459 hicksaa01 97 352 90 11 33.0\n",
  2469. "101069 santada01 91 261 56 0 21.0\n",
  2470. "100994 robinsh01 83 180 45 0 16.0\n",
  2471. "101067 sanomi01 80 279 75 18 52.0\n",
  2472. "100816 nunezed02 72 188 53 4 20.0\n",
  2473. "99988 boyerbl01 68 0 0 0 0.0\n",
  2474. "100270 fienca01 62 0 0 0 0.0\n",
  2475. "100895 perkigl01 60 0 0 0 0.0\n",
  2476. "101240 vargake01 58 175 42 5 17.0\n",
  2477. "100221 duensbr01 55 1 0 0 0.0\n",
  2478. "100701 maytr01 48 3 0 0 0.0\n",
  2479. "100030 buxtoby01 46 129 27 2 6.0\n",
  2480. "100455 herrmch01 45 103 15 2 10.0"
  2481. ]
  2482. },
  2483. "execution_count": 10,
  2484. "metadata": {},
  2485. "output_type": "execute_result"
  2486. }
  2487. ],
  2488. "source": [
  2489. "df_min_2015 = df.loc[(df.yearID == 2015) & (df.teamID == \"MIN\"),\\\n",
  2490. " ['playerID', 'G', 'AB', 'H', 'HR', 'RBI']]\\\n",
  2491. " .sort_values('G', ascending=False)\n",
  2492. "df_min_2015.head(20)"
  2493. ]
  2494. },
  2495. {
  2496. "cell_type": "markdown",
  2497. "metadata": {},
  2498. "source": [
  2499. "We may also do a _multi-sort_ by passing in the list of _columns_ we want sorted. This will sort in the order of the columns provided. For example,"
  2500. ]
  2501. },
  2502. {
  2503. "cell_type": "code",
  2504. "execution_count": 11,
  2505. "metadata": {},
  2506. "outputs": [
  2507. {
  2508. "data": {
  2509. "text/html": [
  2510. "<div>\n",
  2511. "<style>\n",
  2512. " .dataframe thead tr:only-child th {\n",
  2513. " text-align: right;\n",
  2514. " }\n",
  2515. "\n",
  2516. " .dataframe thead th {\n",
  2517. " text-align: left;\n",
  2518. " }\n",
  2519. "\n",
  2520. " .dataframe tbody tr th {\n",
  2521. " vertical-align: top;\n",
  2522. " }\n",
  2523. "</style>\n",
  2524. "<table border=\"1\" class=\"dataframe\">\n",
  2525. " <thead>\n",
  2526. " <tr style=\"text-align: right;\">\n",
  2527. " <th></th>\n",
  2528. " <th>playerID</th>\n",
  2529. " <th>G</th>\n",
  2530. " <th>AB</th>\n",
  2531. " <th>H</th>\n",
  2532. " <th>HR</th>\n",
  2533. " <th>RBI</th>\n",
  2534. " </tr>\n",
  2535. " </thead>\n",
  2536. " <tbody>\n",
  2537. " <tr>\n",
  2538. " <th>101189</th>\n",
  2539. " <td>thielca01</td>\n",
  2540. " <td>6</td>\n",
  2541. " <td>0</td>\n",
  2542. " <td>0</td>\n",
  2543. " <td>0</td>\n",
  2544. " <td>0.0</td>\n",
  2545. " </tr>\n",
  2546. " <tr>\n",
  2547. " <th>99954</th>\n",
  2548. " <td>bernido01</td>\n",
  2549. " <td>4</td>\n",
  2550. " <td>5</td>\n",
  2551. " <td>1</td>\n",
  2552. " <td>0</td>\n",
  2553. " <td>2.0</td>\n",
  2554. " </tr>\n",
  2555. " <tr>\n",
  2556. " <th>100917</th>\n",
  2557. " <td>polanjo01</td>\n",
  2558. " <td>4</td>\n",
  2559. " <td>10</td>\n",
  2560. " <td>3</td>\n",
  2561. " <td>0</td>\n",
  2562. " <td>1.0</td>\n",
  2563. " </tr>\n",
  2564. " <tr>\n",
  2565. " <th>100564</th>\n",
  2566. " <td>keplema01</td>\n",
  2567. " <td>3</td>\n",
  2568. " <td>7</td>\n",
  2569. " <td>1</td>\n",
  2570. " <td>0</td>\n",
  2571. " <td>0.0</td>\n",
  2572. " </tr>\n",
  2573. " <tr>\n",
  2574. " <th>100729</th>\n",
  2575. " <td>meyeral01</td>\n",
  2576. " <td>2</td>\n",
  2577. " <td>0</td>\n",
  2578. " <td>0</td>\n",
  2579. " <td>0</td>\n",
  2580. " <td>0.0</td>\n",
  2581. " </tr>\n",
  2582. " </tbody>\n",
  2583. "</table>\n",
  2584. "</div>"
  2585. ],
  2586. "text/plain": [
  2587. " playerID G AB H HR RBI\n",
  2588. "101189 thielca01 6 0 0 0 0.0\n",
  2589. "99954 bernido01 4 5 1 0 2.0\n",
  2590. "100917 polanjo01 4 10 3 0 1.0\n",
  2591. "100564 keplema01 3 7 1 0 0.0\n",
  2592. "100729 meyeral01 2 0 0 0 0.0"
  2593. ]
  2594. },
  2595. "execution_count": 11,
  2596. "metadata": {},
  2597. "output_type": "execute_result"
  2598. }
  2599. ],
  2600. "source": [
  2601. "df.loc[(df.yearID == 2015) & (df.teamID == \"MIN\"),\\\n",
  2602. " ['playerID', 'G', 'AB', 'H', 'HR', 'RBI']]\\\n",
  2603. " .sort_values(['G', 'HR'], ascending=False).tail()"
  2604. ]
  2605. },
  2606. {
  2607. "cell_type": "markdown",
  2608. "metadata": {},
  2609. "source": [
  2610. "## DataFrame manipulation"
  2611. ]
  2612. },
  2613. {
  2614. "cell_type": "markdown",
  2615. "metadata": {},
  2616. "source": [
  2617. "### Adding and dropping columns"
  2618. ]
  2619. },
  2620. {
  2621. "cell_type": "code",
  2622. "execution_count": 12,
  2623. "metadata": {},
  2624. "outputs": [
  2625. {
  2626. "data": {
  2627. "text/html": [
  2628. "<div>\n",
  2629. "<style>\n",
  2630. " .dataframe thead tr:only-child th {\n",
  2631. " text-align: right;\n",
  2632. " }\n",
  2633. "\n",
  2634. " .dataframe thead th {\n",
  2635. " text-align: left;\n",
  2636. " }\n",
  2637. "\n",
  2638. " .dataframe tbody tr th {\n",
  2639. " vertical-align: top;\n",
  2640. " }\n",
  2641. "</style>\n",
  2642. "<table border=\"1\" class=\"dataframe\">\n",
  2643. " <thead>\n",
  2644. " <tr style=\"text-align: right;\">\n",
  2645. " <th></th>\n",
  2646. " <th>playerID</th>\n",
  2647. " <th>G</th>\n",
  2648. " <th>AB</th>\n",
  2649. " <th>H</th>\n",
  2650. " <th>HR</th>\n",
  2651. " <th>RBI</th>\n",
  2652. " <th>HtoAB</th>\n",
  2653. " </tr>\n",
  2654. " </thead>\n",
  2655. " <tbody>\n",
  2656. " <tr>\n",
  2657. " <th>100696</th>\n",
  2658. " <td>mauerjo01</td>\n",
  2659. " <td>158</td>\n",
  2660. " <td>592</td>\n",
  2661. " <td>157</td>\n",
  2662. " <td>10</td>\n",
  2663. " <td>66.0</td>\n",
  2664. " <td>0</td>\n",
  2665. " </tr>\n",
  2666. " <tr>\n",
  2667. " <th>100215</th>\n",
  2668. " <td>doziebr01</td>\n",
  2669. " <td>157</td>\n",
  2670. " <td>628</td>\n",
  2671. " <td>148</td>\n",
  2672. " <td>28</td>\n",
  2673. " <td>77.0</td>\n",
  2674. " <td>0</td>\n",
  2675. " </tr>\n",
  2676. " <tr>\n",
  2677. " <th>100915</th>\n",
  2678. " <td>plouftr01</td>\n",
  2679. " <td>152</td>\n",
  2680. " <td>573</td>\n",
  2681. " <td>140</td>\n",
  2682. " <td>22</td>\n",
  2683. " <td>86.0</td>\n",
  2684. " <td>0</td>\n",
  2685. " </tr>\n",
  2686. " <tr>\n",
  2687. " <th>100488</th>\n",
  2688. " <td>hunteto01</td>\n",
  2689. " <td>139</td>\n",
  2690. " <td>521</td>\n",
  2691. " <td>125</td>\n",
  2692. " <td>22</td>\n",
  2693. " <td>81.0</td>\n",
  2694. " <td>0</td>\n",
  2695. " </tr>\n",
  2696. " <tr>\n",
  2697. " <th>101164</th>\n",
  2698. " <td>suzukku01</td>\n",
  2699. " <td>131</td>\n",
  2700. " <td>433</td>\n",
  2701. " <td>104</td>\n",
  2702. " <td>5</td>\n",
  2703. " <td>50.0</td>\n",
  2704. " <td>0</td>\n",
  2705. " </tr>\n",
  2706. " </tbody>\n",
  2707. "</table>\n",
  2708. "</div>"
  2709. ],
  2710. "text/plain": [
  2711. " playerID G AB H HR RBI HtoAB\n",
  2712. "100696 mauerjo01 158 592 157 10 66.0 0\n",
  2713. "100215 doziebr01 157 628 148 28 77.0 0\n",
  2714. "100915 plouftr01 152 573 140 22 86.0 0\n",
  2715. "100488 hunteto01 139 521 125 22 81.0 0\n",
  2716. "101164 suzukku01 131 433 104 5 50.0 0"
  2717. ]
  2718. },
  2719. "execution_count": 12,
  2720. "metadata": {},
  2721. "output_type": "execute_result"
  2722. }
  2723. ],
  2724. "source": [
  2725. "df_min_2015.loc[:,'HtoAB'] = 0\n",
  2726. "df_min_2015.head()"
  2727. ]
  2728. },
  2729. {
  2730. "cell_type": "code",
  2731. "execution_count": 13,
  2732. "metadata": {},
  2733. "outputs": [
  2734. {
  2735. "data": {
  2736. "text/html": [
  2737. "<div>\n",
  2738. "<style>\n",
  2739. " .dataframe thead tr:only-child th {\n",
  2740. " text-align: right;\n",
  2741. " }\n",
  2742. "\n",
  2743. " .dataframe thead th {\n",
  2744. " text-align: left;\n",
  2745. " }\n",
  2746. "\n",
  2747. " .dataframe tbody tr th {\n",
  2748. " vertical-align: top;\n",
  2749. " }\n",
  2750. "</style>\n",
  2751. "<table border=\"1\" class=\"dataframe\">\n",
  2752. " <thead>\n",
  2753. " <tr style=\"text-align: right;\">\n",
  2754. " <th></th>\n",
  2755. " <th>playerID</th>\n",
  2756. " <th>G</th>\n",
  2757. " <th>AB</th>\n",
  2758. " <th>H</th>\n",
  2759. " <th>HR</th>\n",
  2760. " <th>RBI</th>\n",
  2761. " </tr>\n",
  2762. " </thead>\n",
  2763. " <tbody>\n",
  2764. " <tr>\n",
  2765. " <th>100696</th>\n",
  2766. " <td>mauerjo01</td>\n",
  2767. " <td>158</td>\n",
  2768. " <td>592</td>\n",
  2769. " <td>157</td>\n",
  2770. " <td>10</td>\n",
  2771. " <td>66.0</td>\n",
  2772. " </tr>\n",
  2773. " <tr>\n",
  2774. " <th>100215</th>\n",
  2775. " <td>doziebr01</td>\n",
  2776. " <td>157</td>\n",
  2777. " <td>628</td>\n",
  2778. " <td>148</td>\n",
  2779. " <td>28</td>\n",
  2780. " <td>77.0</td>\n",
  2781. " </tr>\n",
  2782. " <tr>\n",
  2783. " <th>100915</th>\n",
  2784. " <td>plouftr01</td>\n",
  2785. " <td>152</td>\n",
  2786. " <td>573</td>\n",
  2787. " <td>140</td>\n",
  2788. " <td>22</td>\n",
  2789. " <td>86.0</td>\n",
  2790. " </tr>\n",
  2791. " <tr>\n",
  2792. " <th>100488</th>\n",
  2793. " <td>hunteto01</td>\n",
  2794. " <td>139</td>\n",
  2795. " <td>521</td>\n",
  2796. " <td>125</td>\n",
  2797. " <td>22</td>\n",
  2798. " <td>81.0</td>\n",
  2799. " </tr>\n",
  2800. " <tr>\n",
  2801. " <th>101164</th>\n",
  2802. " <td>suzukku01</td>\n",
  2803. " <td>131</td>\n",
  2804. " <td>433</td>\n",
  2805. " <td>104</td>\n",
  2806. " <td>5</td>\n",
  2807. " <td>50.0</td>\n",
  2808. " </tr>\n",
  2809. " </tbody>\n",
  2810. "</table>\n",
  2811. "</div>"
  2812. ],
  2813. "text/plain": [
  2814. " playerID G AB H HR RBI\n",
  2815. "100696 mauerjo01 158 592 157 10 66.0\n",
  2816. "100215 doziebr01 157 628 148 28 77.0\n",
  2817. "100915 plouftr01 152 573 140 22 86.0\n",
  2818. "100488 hunteto01 139 521 125 22 81.0\n",
  2819. "101164 suzukku01 131 433 104 5 50.0"
  2820. ]
  2821. },
  2822. "execution_count": 13,
  2823. "metadata": {},
  2824. "output_type": "execute_result"
  2825. }
  2826. ],
  2827. "source": [
  2828. "df_min_2015 = df_min_2015.drop('HtoAB', axis=1)\n",
  2829. "df_min_2015.head()"
  2830. ]
  2831. },
  2832. {
  2833. "cell_type": "code",
  2834. "execution_count": 14,
  2835. "metadata": {},
  2836. "outputs": [
  2837. {
  2838. "data": {
  2839. "text/plain": [
  2840. "100696 157\n",
  2841. "100215 148\n",
  2842. "100915 140\n",
  2843. "100488 125\n",
  2844. "101164 104\n",
  2845. "100249 107\n",
  2846. "101023 121\n",
  2847. "100459 90\n",
  2848. "101069 56\n",
  2849. "100994 45\n",
  2850. "Name: H, dtype: int64"
  2851. ]
  2852. },
  2853. "execution_count": 14,
  2854. "metadata": {},
  2855. "output_type": "execute_result"
  2856. }
  2857. ],
  2858. "source": [
  2859. "df_min_2015.H.head(10)"
  2860. ]
  2861. },
  2862. {
  2863. "cell_type": "code",
  2864. "execution_count": 15,
  2865. "metadata": {
  2866. "collapsed": true
  2867. },
  2868. "outputs": [],
  2869. "source": [
  2870. "df_min_2015.loc[:,'HtoAB'] = 0\n",
  2871. "df_min_2015.loc[:,'HtoAB'] = [v.H/v.AB \n",
  2872. " if v.AB > 0 else 0 \n",
  2873. " for r, v in df_min_2015.iterrows()]"
  2874. ]
  2875. },
  2876. {
  2877. "cell_type": "code",
  2878. "execution_count": 16,
  2879. "metadata": {},
  2880. "outputs": [
  2881. {
  2882. "data": {
  2883. "text/html": [
  2884. "<div>\n",
  2885. "<style>\n",
  2886. " .dataframe thead tr:only-child th {\n",
  2887. " text-align: right;\n",
  2888. " }\n",
  2889. "\n",
  2890. " .dataframe thead th {\n",
  2891. " text-align: left;\n",
  2892. " }\n",
  2893. "\n",
  2894. " .dataframe tbody tr th {\n",
  2895. " vertical-align: top;\n",
  2896. " }\n",
  2897. "</style>\n",
  2898. "<table border=\"1\" class=\"dataframe\">\n",
  2899. " <thead>\n",
  2900. " <tr style=\"text-align: right;\">\n",
  2901. " <th></th>\n",
  2902. " <th>playerID</th>\n",
  2903. " <th>G</th>\n",
  2904. " <th>AB</th>\n",
  2905. " <th>H</th>\n",
  2906. " <th>HR</th>\n",
  2907. " <th>RBI</th>\n",
  2908. " <th>HtoAB</th>\n",
  2909. " </tr>\n",
  2910. " </thead>\n",
  2911. " <tbody>\n",
  2912. " <tr>\n",
  2913. " <th>100696</th>\n",
  2914. " <td>mauerjo01</td>\n",
  2915. " <td>158</td>\n",
  2916. " <td>592</td>\n",
  2917. " <td>157</td>\n",
  2918. " <td>10</td>\n",
  2919. " <td>66.0</td>\n",
  2920. " <td>0.265203</td>\n",
  2921. " </tr>\n",
  2922. " <tr>\n",
  2923. " <th>100215</th>\n",
  2924. " <td>doziebr01</td>\n",
  2925. " <td>157</td>\n",
  2926. " <td>628</td>\n",
  2927. " <td>148</td>\n",
  2928. " <td>28</td>\n",
  2929. " <td>77.0</td>\n",
  2930. " <td>0.235669</td>\n",
  2931. " </tr>\n",
  2932. " <tr>\n",
  2933. " <th>100915</th>\n",
  2934. " <td>plouftr01</td>\n",
  2935. " <td>152</td>\n",
  2936. " <td>573</td>\n",
  2937. " <td>140</td>\n",
  2938. " <td>22</td>\n",
  2939. " <td>86.0</td>\n",
  2940. " <td>0.244328</td>\n",
  2941. " </tr>\n",
  2942. " <tr>\n",
  2943. " <th>100488</th>\n",
  2944. " <td>hunteto01</td>\n",
  2945. " <td>139</td>\n",
  2946. " <td>521</td>\n",
  2947. " <td>125</td>\n",
  2948. " <td>22</td>\n",
  2949. " <td>81.0</td>\n",
  2950. " <td>0.239923</td>\n",
  2951. " </tr>\n",
  2952. " <tr>\n",
  2953. " <th>101164</th>\n",
  2954. " <td>suzukku01</td>\n",
  2955. " <td>131</td>\n",
  2956. " <td>433</td>\n",
  2957. " <td>104</td>\n",
  2958. " <td>5</td>\n",
  2959. " <td>50.0</td>\n",
  2960. " <td>0.240185</td>\n",
  2961. " </tr>\n",
  2962. " <tr>\n",
  2963. " <th>100249</th>\n",
  2964. " <td>escobed01</td>\n",
  2965. " <td>127</td>\n",
  2966. " <td>409</td>\n",
  2967. " <td>107</td>\n",
  2968. " <td>12</td>\n",
  2969. " <td>58.0</td>\n",
  2970. " <td>0.261614</td>\n",
  2971. " </tr>\n",
  2972. " <tr>\n",
  2973. " <th>101023</th>\n",
  2974. " <td>rosared01</td>\n",
  2975. " <td>122</td>\n",
  2976. " <td>453</td>\n",
  2977. " <td>121</td>\n",
  2978. " <td>13</td>\n",
  2979. " <td>50.0</td>\n",
  2980. " <td>0.267108</td>\n",
  2981. " </tr>\n",
  2982. " <tr>\n",
  2983. " <th>100459</th>\n",
  2984. " <td>hicksaa01</td>\n",
  2985. " <td>97</td>\n",
  2986. " <td>352</td>\n",
  2987. " <td>90</td>\n",
  2988. " <td>11</td>\n",
  2989. " <td>33.0</td>\n",
  2990. " <td>0.255682</td>\n",
  2991. " </tr>\n",
  2992. " <tr>\n",
  2993. " <th>101069</th>\n",
  2994. " <td>santada01</td>\n",
  2995. " <td>91</td>\n",
  2996. " <td>261</td>\n",
  2997. " <td>56</td>\n",
  2998. " <td>0</td>\n",
  2999. " <td>21.0</td>\n",
  3000. " <td>0.214559</td>\n",
  3001. " </tr>\n",
  3002. " <tr>\n",
  3003. " <th>100994</th>\n",
  3004. " <td>robinsh01</td>\n",
  3005. " <td>83</td>\n",
  3006. " <td>180</td>\n",
  3007. " <td>45</td>\n",
  3008. " <td>0</td>\n",
  3009. " <td>16.0</td>\n",
  3010. " <td>0.250000</td>\n",
  3011. " </tr>\n",
  3012. " </tbody>\n",
  3013. "</table>\n",
  3014. "</div>"
  3015. ],
  3016. "text/plain": [
  3017. " playerID G AB H HR RBI HtoAB\n",
  3018. "100696 mauerjo01 158 592 157 10 66.0 0.265203\n",
  3019. "100215 doziebr01 157 628 148 28 77.0 0.235669\n",
  3020. "100915 plouftr01 152 573 140 22 86.0 0.244328\n",
  3021. "100488 hunteto01 139 521 125 22 81.0 0.239923\n",
  3022. "101164 suzukku01 131 433 104 5 50.0 0.240185\n",
  3023. "100249 escobed01 127 409 107 12 58.0 0.261614\n",
  3024. "101023 rosared01 122 453 121 13 50.0 0.267108\n",
  3025. "100459 hicksaa01 97 352 90 11 33.0 0.255682\n",
  3026. "101069 santada01 91 261 56 0 21.0 0.214559\n",
  3027. "100994 robinsh01 83 180 45 0 16.0 0.250000"
  3028. ]
  3029. },
  3030. "execution_count": 16,
  3031. "metadata": {},
  3032. "output_type": "execute_result"
  3033. }
  3034. ],
  3035. "source": [
  3036. "df_min_2015.head(10)"
  3037. ]
  3038. },
  3039. {
  3040. "cell_type": "code",
  3041. "execution_count": 17,
  3042. "metadata": {},
  3043. "outputs": [
  3044. {
  3045. "data": {
  3046. "text/html": [
  3047. "<div>\n",
  3048. "<style>\n",
  3049. " .dataframe thead tr:only-child th {\n",
  3050. " text-align: right;\n",
  3051. " }\n",
  3052. "\n",
  3053. " .dataframe thead th {\n",
  3054. " text-align: left;\n",
  3055. " }\n",
  3056. "\n",
  3057. " .dataframe tbody tr th {\n",
  3058. " vertical-align: top;\n",
  3059. " }\n",
  3060. "</style>\n",
  3061. "<table border=\"1\" class=\"dataframe\">\n",
  3062. " <thead>\n",
  3063. " <tr style=\"text-align: right;\">\n",
  3064. " <th></th>\n",
  3065. " <th>playerID</th>\n",
  3066. " <th>G</th>\n",
  3067. " <th>AB</th>\n",
  3068. " <th>H</th>\n",
  3069. " <th>HR</th>\n",
  3070. " <th>RBI</th>\n",
  3071. " <th>HtoAB</th>\n",
  3072. " </tr>\n",
  3073. " </thead>\n",
  3074. " <tbody>\n",
  3075. " <tr>\n",
  3076. " <th>101023</th>\n",
  3077. " <td>rosared01</td>\n",
  3078. " <td>122</td>\n",
  3079. " <td>453</td>\n",
  3080. " <td>121</td>\n",
  3081. " <td>13</td>\n",
  3082. " <td>50.0</td>\n",
  3083. " <td>0.267108</td>\n",
  3084. " </tr>\n",
  3085. " <tr>\n",
  3086. " <th>100696</th>\n",
  3087. " <td>mauerjo01</td>\n",
  3088. " <td>158</td>\n",
  3089. " <td>592</td>\n",
  3090. " <td>157</td>\n",
  3091. " <td>10</td>\n",
  3092. " <td>66.0</td>\n",
  3093. " <td>0.265203</td>\n",
  3094. " </tr>\n",
  3095. " <tr>\n",
  3096. " <th>100249</th>\n",
  3097. " <td>escobed01</td>\n",
  3098. " <td>127</td>\n",
  3099. " <td>409</td>\n",
  3100. " <td>107</td>\n",
  3101. " <td>12</td>\n",
  3102. " <td>58.0</td>\n",
  3103. " <td>0.261614</td>\n",
  3104. " </tr>\n",
  3105. " <tr>\n",
  3106. " <th>100459</th>\n",
  3107. " <td>hicksaa01</td>\n",
  3108. " <td>97</td>\n",
  3109. " <td>352</td>\n",
  3110. " <td>90</td>\n",
  3111. " <td>11</td>\n",
  3112. " <td>33.0</td>\n",
  3113. " <td>0.255682</td>\n",
  3114. " </tr>\n",
  3115. " <tr>\n",
  3116. " <th>100994</th>\n",
  3117. " <td>robinsh01</td>\n",
  3118. " <td>83</td>\n",
  3119. " <td>180</td>\n",
  3120. " <td>45</td>\n",
  3121. " <td>0</td>\n",
  3122. " <td>16.0</td>\n",
  3123. " <td>0.250000</td>\n",
  3124. " </tr>\n",
  3125. " <tr>\n",
  3126. " <th>100915</th>\n",
  3127. " <td>plouftr01</td>\n",
  3128. " <td>152</td>\n",
  3129. " <td>573</td>\n",
  3130. " <td>140</td>\n",
  3131. " <td>22</td>\n",
  3132. " <td>86.0</td>\n",
  3133. " <td>0.244328</td>\n",
  3134. " </tr>\n",
  3135. " <tr>\n",
  3136. " <th>101164</th>\n",
  3137. " <td>suzukku01</td>\n",
  3138. " <td>131</td>\n",
  3139. " <td>433</td>\n",
  3140. " <td>104</td>\n",
  3141. " <td>5</td>\n",
  3142. " <td>50.0</td>\n",
  3143. " <td>0.240185</td>\n",
  3144. " </tr>\n",
  3145. " <tr>\n",
  3146. " <th>100488</th>\n",
  3147. " <td>hunteto01</td>\n",
  3148. " <td>139</td>\n",
  3149. " <td>521</td>\n",
  3150. " <td>125</td>\n",
  3151. " <td>22</td>\n",
  3152. " <td>81.0</td>\n",
  3153. " <td>0.239923</td>\n",
  3154. " </tr>\n",
  3155. " <tr>\n",
  3156. " <th>100215</th>\n",
  3157. " <td>doziebr01</td>\n",
  3158. " <td>157</td>\n",
  3159. " <td>628</td>\n",
  3160. " <td>148</td>\n",
  3161. " <td>28</td>\n",
  3162. " <td>77.0</td>\n",
  3163. " <td>0.235669</td>\n",
  3164. " </tr>\n",
  3165. " <tr>\n",
  3166. " <th>101069</th>\n",
  3167. " <td>santada01</td>\n",
  3168. " <td>91</td>\n",
  3169. " <td>261</td>\n",
  3170. " <td>56</td>\n",
  3171. " <td>0</td>\n",
  3172. " <td>21.0</td>\n",
  3173. " <td>0.214559</td>\n",
  3174. " </tr>\n",
  3175. " </tbody>\n",
  3176. "</table>\n",
  3177. "</div>"
  3178. ],
  3179. "text/plain": [
  3180. " playerID G AB H HR RBI HtoAB\n",
  3181. "101023 rosared01 122 453 121 13 50.0 0.267108\n",
  3182. "100696 mauerjo01 158 592 157 10 66.0 0.265203\n",
  3183. "100249 escobed01 127 409 107 12 58.0 0.261614\n",
  3184. "100459 hicksaa01 97 352 90 11 33.0 0.255682\n",
  3185. "100994 robinsh01 83 180 45 0 16.0 0.250000\n",
  3186. "100915 plouftr01 152 573 140 22 86.0 0.244328\n",
  3187. "101164 suzukku01 131 433 104 5 50.0 0.240185\n",
  3188. "100488 hunteto01 139 521 125 22 81.0 0.239923\n",
  3189. "100215 doziebr01 157 628 148 28 77.0 0.235669\n",
  3190. "101069 santada01 91 261 56 0 21.0 0.214559"
  3191. ]
  3192. },
  3193. "execution_count": 17,
  3194. "metadata": {},
  3195. "output_type": "execute_result"
  3196. }
  3197. ],
  3198. "source": [
  3199. "df_min_2015[df_min_2015.G>80].sort_values('HtoAB', ascending=False)"
  3200. ]
  3201. },
  3202. {
  3203. "cell_type": "code",
  3204. "execution_count": 18,
  3205. "metadata": {},
  3206. "outputs": [
  3207. {
  3208. "data": {
  3209. "text/html": [
  3210. "<div>\n",
  3211. "<style>\n",
  3212. " .dataframe thead tr:only-child th {\n",
  3213. " text-align: right;\n",
  3214. " }\n",
  3215. "\n",
  3216. " .dataframe thead th {\n",
  3217. " text-align: left;\n",
  3218. " }\n",
  3219. "\n",
  3220. " .dataframe tbody tr th {\n",
  3221. " vertical-align: top;\n",
  3222. " }\n",
  3223. "</style>\n",
  3224. "<table border=\"1\" class=\"dataframe\">\n",
  3225. " <thead>\n",
  3226. " <tr style=\"text-align: right;\">\n",
  3227. " <th></th>\n",
  3228. " <th>playerID</th>\n",
  3229. " <th>HtoAB</th>\n",
  3230. " <th>AB</th>\n",
  3231. " <th>H</th>\n",
  3232. " <th>HR</th>\n",
  3233. " <th>RBI</th>\n",
  3234. " <th>G</th>\n",
  3235. " </tr>\n",
  3236. " </thead>\n",
  3237. " <tbody>\n",
  3238. " <tr>\n",
  3239. " <th>100696</th>\n",
  3240. " <td>mauerjo01</td>\n",
  3241. " <td>0.265203</td>\n",
  3242. " <td>592</td>\n",
  3243. " <td>157</td>\n",
  3244. " <td>10</td>\n",
  3245. " <td>66.0</td>\n",
  3246. " <td>158</td>\n",
  3247. " </tr>\n",
  3248. " <tr>\n",
  3249. " <th>100215</th>\n",
  3250. " <td>doziebr01</td>\n",
  3251. " <td>0.235669</td>\n",
  3252. " <td>628</td>\n",
  3253. " <td>148</td>\n",
  3254. " <td>28</td>\n",
  3255. " <td>77.0</td>\n",
  3256. " <td>157</td>\n",
  3257. " </tr>\n",
  3258. " <tr>\n",
  3259. " <th>100915</th>\n",
  3260. " <td>plouftr01</td>\n",
  3261. " <td>0.244328</td>\n",
  3262. " <td>573</td>\n",
  3263. " <td>140</td>\n",
  3264. " <td>22</td>\n",
  3265. " <td>86.0</td>\n",
  3266. " <td>152</td>\n",
  3267. " </tr>\n",
  3268. " <tr>\n",
  3269. " <th>100488</th>\n",
  3270. " <td>hunteto01</td>\n",
  3271. " <td>0.239923</td>\n",
  3272. " <td>521</td>\n",
  3273. " <td>125</td>\n",
  3274. " <td>22</td>\n",
  3275. " <td>81.0</td>\n",
  3276. " <td>139</td>\n",
  3277. " </tr>\n",
  3278. " <tr>\n",
  3279. " <th>101164</th>\n",
  3280. " <td>suzukku01</td>\n",
  3281. " <td>0.240185</td>\n",
  3282. " <td>433</td>\n",
  3283. " <td>104</td>\n",
  3284. " <td>5</td>\n",
  3285. " <td>50.0</td>\n",
  3286. " <td>131</td>\n",
  3287. " </tr>\n",
  3288. " </tbody>\n",
  3289. "</table>\n",
  3290. "</div>"
  3291. ],
  3292. "text/plain": [
  3293. " playerID HtoAB AB H HR RBI G\n",
  3294. "100696 mauerjo01 0.265203 592 157 10 66.0 158\n",
  3295. "100215 doziebr01 0.235669 628 148 28 77.0 157\n",
  3296. "100915 plouftr01 0.244328 573 140 22 86.0 152\n",
  3297. "100488 hunteto01 0.239923 521 125 22 81.0 139\n",
  3298. "101164 suzukku01 0.240185 433 104 5 50.0 131"
  3299. ]
  3300. },
  3301. "execution_count": 18,
  3302. "metadata": {},
  3303. "output_type": "execute_result"
  3304. }
  3305. ],
  3306. "source": [
  3307. "df_min_2015 = df_min_2015.reindex(columns=['playerID', 'HtoAB', 'AB', 'H', 'HR', 'RBI', 'G'])\n",
  3308. "df_min_2015.head()"
  3309. ]
  3310. },
  3311. {
  3312. "cell_type": "markdown",
  3313. "metadata": {},
  3314. "source": [
  3315. "Finally, we can return our DataFrame back to its original columns (and order) by reindexing again. Notice, also that we can effectively perform a `drop()` by doing this, though the syntax with `reindex()` is more verbose."
  3316. ]
  3317. },
  3318. {
  3319. "cell_type": "code",
  3320. "execution_count": 19,
  3321. "metadata": {},
  3322. "outputs": [
  3323. {
  3324. "data": {
  3325. "text/html": [
  3326. "<div>\n",
  3327. "<style>\n",
  3328. " .dataframe thead tr:only-child th {\n",
  3329. " text-align: right;\n",
  3330. " }\n",
  3331. "\n",
  3332. " .dataframe thead th {\n",
  3333. " text-align: left;\n",
  3334. " }\n",
  3335. "\n",
  3336. " .dataframe tbody tr th {\n",
  3337. " vertical-align: top;\n",
  3338. " }\n",
  3339. "</style>\n",
  3340. "<table border=\"1\" class=\"dataframe\">\n",
  3341. " <thead>\n",
  3342. " <tr style=\"text-align: right;\">\n",
  3343. " <th></th>\n",
  3344. " <th>playerID</th>\n",
  3345. " <th>G</th>\n",
  3346. " <th>AB</th>\n",
  3347. " <th>H</th>\n",
  3348. " <th>HR</th>\n",
  3349. " <th>RBI</th>\n",
  3350. " </tr>\n",
  3351. " </thead>\n",
  3352. " <tbody>\n",
  3353. " <tr>\n",
  3354. " <th>100696</th>\n",
  3355. " <td>mauerjo01</td>\n",
  3356. " <td>158</td>\n",
  3357. " <td>592</td>\n",
  3358. " <td>157</td>\n",
  3359. " <td>10</td>\n",
  3360. " <td>66.0</td>\n",
  3361. " </tr>\n",
  3362. " <tr>\n",
  3363. " <th>100215</th>\n",
  3364. " <td>doziebr01</td>\n",
  3365. " <td>157</td>\n",
  3366. " <td>628</td>\n",
  3367. " <td>148</td>\n",
  3368. " <td>28</td>\n",
  3369. " <td>77.0</td>\n",
  3370. " </tr>\n",
  3371. " <tr>\n",
  3372. " <th>100915</th>\n",
  3373. " <td>plouftr01</td>\n",
  3374. " <td>152</td>\n",
  3375. " <td>573</td>\n",
  3376. " <td>140</td>\n",
  3377. " <td>22</td>\n",
  3378. " <td>86.0</td>\n",
  3379. " </tr>\n",
  3380. " <tr>\n",
  3381. " <th>100488</th>\n",
  3382. " <td>hunteto01</td>\n",
  3383. " <td>139</td>\n",
  3384. " <td>521</td>\n",
  3385. " <td>125</td>\n",
  3386. " <td>22</td>\n",
  3387. " <td>81.0</td>\n",
  3388. " </tr>\n",
  3389. " <tr>\n",
  3390. " <th>101164</th>\n",
  3391. " <td>suzukku01</td>\n",
  3392. " <td>131</td>\n",
  3393. " <td>433</td>\n",
  3394. " <td>104</td>\n",
  3395. " <td>5</td>\n",
  3396. " <td>50.0</td>\n",
  3397. " </tr>\n",
  3398. " </tbody>\n",
  3399. "</table>\n",
  3400. "</div>"
  3401. ],
  3402. "text/plain": [
  3403. " playerID G AB H HR RBI\n",
  3404. "100696 mauerjo01 158 592 157 10 66.0\n",
  3405. "100215 doziebr01 157 628 148 28 77.0\n",
  3406. "100915 plouftr01 152 573 140 22 86.0\n",
  3407. "100488 hunteto01 139 521 125 22 81.0\n",
  3408. "101164 suzukku01 131 433 104 5 50.0"
  3409. ]
  3410. },
  3411. "execution_count": 19,
  3412. "metadata": {},
  3413. "output_type": "execute_result"
  3414. }
  3415. ],
  3416. "source": [
  3417. "df_min_2015 = df_min_2015.reindex(columns=['playerID', 'G', 'AB', 'H', 'HR', 'RBI'])\n",
  3418. "df_min_2015.head()"
  3419. ]
  3420. },
  3421. {
  3422. "cell_type": "markdown",
  3423. "metadata": {},
  3424. "source": [
  3425. "### Adding and dropping rows"
  3426. ]
  3427. },
  3428. {
  3429. "cell_type": "markdown",
  3430. "metadata": {},
  3431. "source": [
  3432. "Adding rows can be achieved using `loc[]` and setting the new index to a dictionary of values using the column labels as keys."
  3433. ]
  3434. },
  3435. {
  3436. "cell_type": "code",
  3437. "execution_count": 20,
  3438. "metadata": {},
  3439. "outputs": [
  3440. {
  3441. "data": {
  3442. "text/html": [
  3443. "<div>\n",
  3444. "<style>\n",
  3445. " .dataframe thead tr:only-child th {\n",
  3446. " text-align: right;\n",
  3447. " }\n",
  3448. "\n",
  3449. " .dataframe thead th {\n",
  3450. " text-align: left;\n",
  3451. " }\n",
  3452. "\n",
  3453. " .dataframe tbody tr th {\n",
  3454. " vertical-align: top;\n",
  3455. " }\n",
  3456. "</style>\n",
  3457. "<table border=\"1\" class=\"dataframe\">\n",
  3458. " <thead>\n",
  3459. " <tr style=\"text-align: right;\">\n",
  3460. " <th></th>\n",
  3461. " <th>playerID</th>\n",
  3462. " <th>G</th>\n",
  3463. " <th>AB</th>\n",
  3464. " <th>H</th>\n",
  3465. " <th>HR</th>\n",
  3466. " <th>RBI</th>\n",
  3467. " </tr>\n",
  3468. " </thead>\n",
  3469. " <tbody>\n",
  3470. " <tr>\n",
  3471. " <th>100917</th>\n",
  3472. " <td>polanjo01</td>\n",
  3473. " <td>4</td>\n",
  3474. " <td>10</td>\n",
  3475. " <td>3</td>\n",
  3476. " <td>0</td>\n",
  3477. " <td>1</td>\n",
  3478. " </tr>\n",
  3479. " <tr>\n",
  3480. " <th>99954</th>\n",
  3481. " <td>bernido01</td>\n",
  3482. " <td>4</td>\n",
  3483. " <td>5</td>\n",
  3484. " <td>1</td>\n",
  3485. " <td>0</td>\n",
  3486. " <td>2</td>\n",
  3487. " </tr>\n",
  3488. " <tr>\n",
  3489. " <th>100564</th>\n",
  3490. " <td>keplema01</td>\n",
  3491. " <td>3</td>\n",
  3492. " <td>7</td>\n",
  3493. " <td>1</td>\n",
  3494. " <td>0</td>\n",
  3495. " <td>0</td>\n",
  3496. " </tr>\n",
  3497. " <tr>\n",
  3498. " <th>100729</th>\n",
  3499. " <td>meyeral01</td>\n",
  3500. " <td>2</td>\n",
  3501. " <td>0</td>\n",
  3502. " <td>0</td>\n",
  3503. " <td>0</td>\n",
  3504. " <td>0</td>\n",
  3505. " </tr>\n",
  3506. " <tr>\n",
  3507. " <th>200000</th>\n",
  3508. " <td>keith01</td>\n",
  3509. " <td>0</td>\n",
  3510. " <td>0</td>\n",
  3511. " <td>0</td>\n",
  3512. " <td>0</td>\n",
  3513. " <td>0</td>\n",
  3514. " </tr>\n",
  3515. " </tbody>\n",
  3516. "</table>\n",
  3517. "</div>"
  3518. ],
  3519. "text/plain": [
  3520. " playerID G AB H HR RBI\n",
  3521. "100917 polanjo01 4 10 3 0 1\n",
  3522. "99954 bernido01 4 5 1 0 2\n",
  3523. "100564 keplema01 3 7 1 0 0\n",
  3524. "100729 meyeral01 2 0 0 0 0\n",
  3525. "200000 keith01 0 0 0 0 0"
  3526. ]
  3527. },
  3528. "execution_count": 20,
  3529. "metadata": {},
  3530. "output_type": "execute_result"
  3531. }
  3532. ],
  3533. "source": [
  3534. "df_min_2015.loc[200000] = \\\n",
  3535. " { 'playerID': 'keith01',\n",
  3536. " 'RBI': '0',\n",
  3537. " 'G': '0',\n",
  3538. " 'H': '0',\n",
  3539. " 'HR': '0',\n",
  3540. " 'AB': '0' }\n",
  3541. " \n",
  3542. "df_min_2015.tail()"
  3543. ]
  3544. },
  3545. {
  3546. "cell_type": "markdown",
  3547. "metadata": {},
  3548. "source": [
  3549. "It is also the same with lists and tuples."
  3550. ]
  3551. },
  3552. {
  3553. "cell_type": "code",
  3554. "execution_count": 21,
  3555. "metadata": {},
  3556. "outputs": [
  3557. {
  3558. "data": {
  3559. "text/html": [
  3560. "<div>\n",
  3561. "<style>\n",
  3562. " .dataframe thead tr:only-child th {\n",
  3563. " text-align: right;\n",
  3564. " }\n",
  3565. "\n",
  3566. " .dataframe thead th {\n",
  3567. " text-align: left;\n",
  3568. " }\n",
  3569. "\n",
  3570. " .dataframe tbody tr th {\n",
  3571. " vertical-align: top;\n",
  3572. " }\n",
  3573. "</style>\n",
  3574. "<table border=\"1\" class=\"dataframe\">\n",
  3575. " <thead>\n",
  3576. " <tr style=\"text-align: right;\">\n",
  3577. " <th></th>\n",
  3578. " <th>playerID</th>\n",
  3579. " <th>G</th>\n",
  3580. " <th>AB</th>\n",
  3581. " <th>H</th>\n",
  3582. " <th>HR</th>\n",
  3583. " <th>RBI</th>\n",
  3584. " </tr>\n",
  3585. " </thead>\n",
  3586. " <tbody>\n",
  3587. " <tr>\n",
  3588. " <th>99954</th>\n",
  3589. " <td>bernido01</td>\n",
  3590. " <td>4</td>\n",
  3591. " <td>5</td>\n",
  3592. " <td>1</td>\n",
  3593. " <td>0</td>\n",
  3594. " <td>2</td>\n",
  3595. " </tr>\n",
  3596. " <tr>\n",
  3597. " <th>100564</th>\n",
  3598. " <td>keplema01</td>\n",
  3599. " <td>3</td>\n",
  3600. " <td>7</td>\n",
  3601. " <td>1</td>\n",
  3602. " <td>0</td>\n",
  3603. " <td>0</td>\n",
  3604. " </tr>\n",
  3605. " <tr>\n",
  3606. " <th>100729</th>\n",
  3607. " <td>meyeral01</td>\n",
  3608. " <td>2</td>\n",
  3609. " <td>0</td>\n",
  3610. " <td>0</td>\n",
  3611. " <td>0</td>\n",
  3612. " <td>0</td>\n",
  3613. " </tr>\n",
  3614. " <tr>\n",
  3615. " <th>200000</th>\n",
  3616. " <td>keith01</td>\n",
  3617. " <td>1</td>\n",
  3618. " <td>1</td>\n",
  3619. " <td>1</td>\n",
  3620. " <td>1</td>\n",
  3621. " <td>1</td>\n",
  3622. " </tr>\n",
  3623. " <tr>\n",
  3624. " <th>200001</th>\n",
  3625. " <td>keith02</td>\n",
  3626. " <td>1</td>\n",
  3627. " <td>1</td>\n",
  3628. " <td>1</td>\n",
  3629. " <td>1</td>\n",
  3630. " <td>1</td>\n",
  3631. " </tr>\n",
  3632. " </tbody>\n",
  3633. "</table>\n",
  3634. "</div>"
  3635. ],
  3636. "text/plain": [
  3637. " playerID G AB H HR RBI\n",
  3638. "99954 bernido01 4 5 1 0 2\n",
  3639. "100564 keplema01 3 7 1 0 0\n",
  3640. "100729 meyeral01 2 0 0 0 0\n",
  3641. "200000 keith01 1 1 1 1 1\n",
  3642. "200001 keith02 1 1 1 1 1"
  3643. ]
  3644. },
  3645. "execution_count": 21,
  3646. "metadata": {},
  3647. "output_type": "execute_result"
  3648. }
  3649. ],
  3650. "source": [
  3651. "df_min_2015.loc[200000] = ('keith01', 1, 1, 1, 1, 1)\n",
  3652. "df_min_2015.loc[200001] = ['keith02', 1, 1, 1, 1, 1]\n",
  3653. "\n",
  3654. "df_min_2015.tail()"
  3655. ]
  3656. },
  3657. {
  3658. "cell_type": "markdown",
  3659. "metadata": {},
  3660. "source": [
  3661. "Note that we can drop a number of rows at a time by passing a list of the indices we'd like dropped."
  3662. ]
  3663. },
  3664. {
  3665. "cell_type": "code",
  3666. "execution_count": 22,
  3667. "metadata": {},
  3668. "outputs": [
  3669. {
  3670. "data": {
  3671. "text/html": [
  3672. "<div>\n",
  3673. "<style>\n",
  3674. " .dataframe thead tr:only-child th {\n",
  3675. " text-align: right;\n",
  3676. " }\n",
  3677. "\n",
  3678. " .dataframe thead th {\n",
  3679. " text-align: left;\n",
  3680. " }\n",
  3681. "\n",
  3682. " .dataframe tbody tr th {\n",
  3683. " vertical-align: top;\n",
  3684. " }\n",
  3685. "</style>\n",
  3686. "<table border=\"1\" class=\"dataframe\">\n",
  3687. " <thead>\n",
  3688. " <tr style=\"text-align: right;\">\n",
  3689. " <th></th>\n",
  3690. " <th>playerID</th>\n",
  3691. " <th>G</th>\n",
  3692. " <th>AB</th>\n",
  3693. " <th>H</th>\n",
  3694. " <th>HR</th>\n",
  3695. " <th>RBI</th>\n",
  3696. " </tr>\n",
  3697. " </thead>\n",
  3698. " <tbody>\n",
  3699. " <tr>\n",
  3700. " <th>101189</th>\n",
  3701. " <td>thielca01</td>\n",
  3702. " <td>6</td>\n",
  3703. " <td>0</td>\n",
  3704. " <td>0</td>\n",
  3705. " <td>0</td>\n",
  3706. " <td>0</td>\n",
  3707. " </tr>\n",
  3708. " <tr>\n",
  3709. " <th>100917</th>\n",
  3710. " <td>polanjo01</td>\n",
  3711. " <td>4</td>\n",
  3712. " <td>10</td>\n",
  3713. " <td>3</td>\n",
  3714. " <td>0</td>\n",
  3715. " <td>1</td>\n",
  3716. " </tr>\n",
  3717. " <tr>\n",
  3718. " <th>99954</th>\n",
  3719. " <td>bernido01</td>\n",
  3720. " <td>4</td>\n",
  3721. " <td>5</td>\n",
  3722. " <td>1</td>\n",
  3723. " <td>0</td>\n",
  3724. " <td>2</td>\n",
  3725. " </tr>\n",
  3726. " <tr>\n",
  3727. " <th>100564</th>\n",
  3728. " <td>keplema01</td>\n",
  3729. " <td>3</td>\n",
  3730. " <td>7</td>\n",
  3731. " <td>1</td>\n",
  3732. " <td>0</td>\n",
  3733. " <td>0</td>\n",
  3734. " </tr>\n",
  3735. " <tr>\n",
  3736. " <th>100729</th>\n",
  3737. " <td>meyeral01</td>\n",
  3738. " <td>2</td>\n",
  3739. " <td>0</td>\n",
  3740. " <td>0</td>\n",
  3741. " <td>0</td>\n",
  3742. " <td>0</td>\n",
  3743. " </tr>\n",
  3744. " </tbody>\n",
  3745. "</table>\n",
  3746. "</div>"
  3747. ],
  3748. "text/plain": [
  3749. " playerID G AB H HR RBI\n",
  3750. "101189 thielca01 6 0 0 0 0\n",
  3751. "100917 polanjo01 4 10 3 0 1\n",
  3752. "99954 bernido01 4 5 1 0 2\n",
  3753. "100564 keplema01 3 7 1 0 0\n",
  3754. "100729 meyeral01 2 0 0 0 0"
  3755. ]
  3756. },
  3757. "execution_count": 22,
  3758. "metadata": {},
  3759. "output_type": "execute_result"
  3760. }
  3761. ],
  3762. "source": [
  3763. "df_min_2015 = df_min_2015.drop([200000, 200001], axis=0)\n",
  3764. "df_min_2015.tail()"
  3765. ]
  3766. },
  3767. {
  3768. "cell_type": "markdown",
  3769. "metadata": {},
  3770. "source": [
  3771. "Similar results can be achieved using [`append()`](http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.append.html#pandas.DataFrame.append). With append, you can append, Series, DataFrames and/or a list of these."
  3772. ]
  3773. },
  3774. {
  3775. "cell_type": "code",
  3776. "execution_count": 23,
  3777. "metadata": {},
  3778. "outputs": [
  3779. {
  3780. "data": {
  3781. "text/html": [
  3782. "<div>\n",
  3783. "<style>\n",
  3784. " .dataframe thead tr:only-child th {\n",
  3785. " text-align: right;\n",
  3786. " }\n",
  3787. "\n",
  3788. " .dataframe thead th {\n",
  3789. " text-align: left;\n",
  3790. " }\n",
  3791. "\n",
  3792. " .dataframe tbody tr th {\n",
  3793. " vertical-align: top;\n",
  3794. " }\n",
  3795. "</style>\n",
  3796. "<table border=\"1\" class=\"dataframe\">\n",
  3797. " <thead>\n",
  3798. " <tr style=\"text-align: right;\">\n",
  3799. " <th></th>\n",
  3800. " <th>playerID</th>\n",
  3801. " <th>G</th>\n",
  3802. " <th>AB</th>\n",
  3803. " <th>H</th>\n",
  3804. " <th>HR</th>\n",
  3805. " <th>RBI</th>\n",
  3806. " </tr>\n",
  3807. " </thead>\n",
  3808. " <tbody>\n",
  3809. " <tr>\n",
  3810. " <th>100917</th>\n",
  3811. " <td>polanjo01</td>\n",
  3812. " <td>4</td>\n",
  3813. " <td>10</td>\n",
  3814. " <td>3</td>\n",
  3815. " <td>0</td>\n",
  3816. " <td>1</td>\n",
  3817. " </tr>\n",
  3818. " <tr>\n",
  3819. " <th>99954</th>\n",
  3820. " <td>bernido01</td>\n",
  3821. " <td>4</td>\n",
  3822. " <td>5</td>\n",
  3823. " <td>1</td>\n",
  3824. " <td>0</td>\n",
  3825. " <td>2</td>\n",
  3826. " </tr>\n",
  3827. " <tr>\n",
  3828. " <th>100564</th>\n",
  3829. " <td>keplema01</td>\n",
  3830. " <td>3</td>\n",
  3831. " <td>7</td>\n",
  3832. " <td>1</td>\n",
  3833. " <td>0</td>\n",
  3834. " <td>0</td>\n",
  3835. " </tr>\n",
  3836. " <tr>\n",
  3837. " <th>100729</th>\n",
  3838. " <td>meyeral01</td>\n",
  3839. " <td>2</td>\n",
  3840. " <td>0</td>\n",
  3841. " <td>0</td>\n",
  3842. " <td>0</td>\n",
  3843. " <td>0</td>\n",
  3844. " </tr>\n",
  3845. " <tr>\n",
  3846. " <th>200000</th>\n",
  3847. " <td>keith01</td>\n",
  3848. " <td>0</td>\n",
  3849. " <td>0</td>\n",
  3850. " <td>0</td>\n",
  3851. " <td>0</td>\n",
  3852. " <td>0</td>\n",
  3853. " </tr>\n",
  3854. " </tbody>\n",
  3855. "</table>\n",
  3856. "</div>"
  3857. ],
  3858. "text/plain": [
  3859. " playerID G AB H HR RBI\n",
  3860. "100917 polanjo01 4 10 3 0 1\n",
  3861. "99954 bernido01 4 5 1 0 2\n",
  3862. "100564 keplema01 3 7 1 0 0\n",
  3863. "100729 meyeral01 2 0 0 0 0\n",
  3864. "200000 keith01 0 0 0 0 0"
  3865. ]
  3866. },
  3867. "execution_count": 23,
  3868. "metadata": {},
  3869. "output_type": "execute_result"
  3870. }
  3871. ],
  3872. "source": [
  3873. "df_min_2015.append(\n",
  3874. " pd.Series( \n",
  3875. " {'playerID': 'keith01', \n",
  3876. " 'G': 0, \n",
  3877. " 'AB': 0, \n",
  3878. " 'H':0, \n",
  3879. " 'HR': 0, \n",
  3880. " 'RBI': 0}, name='200000')).tail()"
  3881. ]
  3882. },
  3883. {
  3884. "cell_type": "code",
  3885. "execution_count": 24,
  3886. "metadata": {},
  3887. "outputs": [
  3888. {
  3889. "data": {
  3890. "text/html": [
  3891. "<div>\n",
  3892. "<style>\n",
  3893. " .dataframe thead tr:only-child th {\n",
  3894. " text-align: right;\n",
  3895. " }\n",
  3896. "\n",
  3897. " .dataframe thead th {\n",
  3898. " text-align: left;\n",
  3899. " }\n",
  3900. "\n",
  3901. " .dataframe tbody tr th {\n",
  3902. " vertical-align: top;\n",
  3903. " }\n",
  3904. "</style>\n",
  3905. "<table border=\"1\" class=\"dataframe\">\n",
  3906. " <thead>\n",
  3907. " <tr style=\"text-align: right;\">\n",
  3908. " <th></th>\n",
  3909. " <th>playerID</th>\n",
  3910. " <th>G</th>\n",
  3911. " <th>AB</th>\n",
  3912. " <th>H</th>\n",
  3913. " <th>HR</th>\n",
  3914. " <th>RBI</th>\n",
  3915. " </tr>\n",
  3916. " </thead>\n",
  3917. " <tbody>\n",
  3918. " <tr>\n",
  3919. " <th>100696</th>\n",
  3920. " <td>mauerjo01</td>\n",
  3921. " <td>158</td>\n",
  3922. " <td>592</td>\n",
  3923. " <td>157</td>\n",
  3924. " <td>10</td>\n",
  3925. " <td>66</td>\n",
  3926. " </tr>\n",
  3927. " <tr>\n",
  3928. " <th>100215</th>\n",
  3929. " <td>doziebr01</td>\n",
  3930. " <td>157</td>\n",
  3931. " <td>628</td>\n",
  3932. " <td>148</td>\n",
  3933. " <td>28</td>\n",
  3934. " <td>77</td>\n",
  3935. " </tr>\n",
  3936. " <tr>\n",
  3937. " <th>100915</th>\n",
  3938. " <td>plouftr01</td>\n",
  3939. " <td>152</td>\n",
  3940. " <td>573</td>\n",
  3941. " <td>140</td>\n",
  3942. " <td>22</td>\n",
  3943. " <td>86</td>\n",
  3944. " </tr>\n",
  3945. " <tr>\n",
  3946. " <th>100488</th>\n",
  3947. " <td>hunteto01</td>\n",
  3948. " <td>139</td>\n",
  3949. " <td>521</td>\n",
  3950. " <td>125</td>\n",
  3951. " <td>22</td>\n",
  3952. " <td>81</td>\n",
  3953. " </tr>\n",
  3954. " <tr>\n",
  3955. " <th>101164</th>\n",
  3956. " <td>suzukku01</td>\n",
  3957. " <td>131</td>\n",
  3958. " <td>433</td>\n",
  3959. " <td>104</td>\n",
  3960. " <td>5</td>\n",
  3961. " <td>50</td>\n",
  3962. " </tr>\n",
  3963. " <tr>\n",
  3964. " <th>101189</th>\n",
  3965. " <td>thielca01</td>\n",
  3966. " <td>6</td>\n",
  3967. " <td>0</td>\n",
  3968. " <td>0</td>\n",
  3969. " <td>0</td>\n",
  3970. " <td>0</td>\n",
  3971. " </tr>\n",
  3972. " <tr>\n",
  3973. " <th>100917</th>\n",
  3974. " <td>polanjo01</td>\n",
  3975. " <td>4</td>\n",
  3976. " <td>10</td>\n",
  3977. " <td>3</td>\n",
  3978. " <td>0</td>\n",
  3979. " <td>1</td>\n",
  3980. " </tr>\n",
  3981. " <tr>\n",
  3982. " <th>99954</th>\n",
  3983. " <td>bernido01</td>\n",
  3984. " <td>4</td>\n",
  3985. " <td>5</td>\n",
  3986. " <td>1</td>\n",
  3987. " <td>0</td>\n",
  3988. " <td>2</td>\n",
  3989. " </tr>\n",
  3990. " <tr>\n",
  3991. " <th>100564</th>\n",
  3992. " <td>keplema01</td>\n",
  3993. " <td>3</td>\n",
  3994. " <td>7</td>\n",
  3995. " <td>1</td>\n",
  3996. " <td>0</td>\n",
  3997. " <td>0</td>\n",
  3998. " </tr>\n",
  3999. " <tr>\n",
  4000. " <th>100729</th>\n",
  4001. " <td>meyeral01</td>\n",
  4002. " <td>2</td>\n",
  4003. " <td>0</td>\n",
  4004. " <td>0</td>\n",
  4005. " <td>0</td>\n",
  4006. " <td>0</td>\n",
  4007. " </tr>\n",
  4008. " </tbody>\n",
  4009. "</table>\n",
  4010. "</div>"
  4011. ],
  4012. "text/plain": [
  4013. " playerID G AB H HR RBI\n",
  4014. "100696 mauerjo01 158 592 157 10 66\n",
  4015. "100215 doziebr01 157 628 148 28 77\n",
  4016. "100915 plouftr01 152 573 140 22 86\n",
  4017. "100488 hunteto01 139 521 125 22 81\n",
  4018. "101164 suzukku01 131 433 104 5 50\n",
  4019. "101189 thielca01 6 0 0 0 0\n",
  4020. "100917 polanjo01 4 10 3 0 1\n",
  4021. "99954 bernido01 4 5 1 0 2\n",
  4022. "100564 keplema01 3 7 1 0 0\n",
  4023. "100729 meyeral01 2 0 0 0 0"
  4024. ]
  4025. },
  4026. "execution_count": 24,
  4027. "metadata": {},
  4028. "output_type": "execute_result"
  4029. }
  4030. ],
  4031. "source": [
  4032. "df_min_2015[:5].append(df_min_2015[-5:])"
  4033. ]
  4034. },
  4035. {
  4036. "cell_type": "code",
  4037. "execution_count": 25,
  4038. "metadata": {},
  4039. "outputs": [
  4040. {
  4041. "data": {
  4042. "text/html": [
  4043. "<div>\n",
  4044. "<style>\n",
  4045. " .dataframe thead tr:only-child th {\n",
  4046. " text-align: right;\n",
  4047. " }\n",
  4048. "\n",
  4049. " .dataframe thead th {\n",
  4050. " text-align: left;\n",
  4051. " }\n",
  4052. "\n",
  4053. " .dataframe tbody tr th {\n",
  4054. " vertical-align: top;\n",
  4055. " }\n",
  4056. "</style>\n",
  4057. "<table border=\"1\" class=\"dataframe\">\n",
  4058. " <thead>\n",
  4059. " <tr style=\"text-align: right;\">\n",
  4060. " <th></th>\n",
  4061. " <th>playerID</th>\n",
  4062. " <th>G</th>\n",
  4063. " <th>AB</th>\n",
  4064. " <th>H</th>\n",
  4065. " <th>HR</th>\n",
  4066. " <th>RBI</th>\n",
  4067. " </tr>\n",
  4068. " </thead>\n",
  4069. " <tbody>\n",
  4070. " <tr>\n",
  4071. " <th>100696</th>\n",
  4072. " <td>mauerjo01</td>\n",
  4073. " <td>158</td>\n",
  4074. " <td>592</td>\n",
  4075. " <td>157</td>\n",
  4076. " <td>10</td>\n",
  4077. " <td>66</td>\n",
  4078. " </tr>\n",
  4079. " <tr>\n",
  4080. " <th>100215</th>\n",
  4081. " <td>doziebr01</td>\n",
  4082. " <td>157</td>\n",
  4083. " <td>628</td>\n",
  4084. " <td>148</td>\n",
  4085. " <td>28</td>\n",
  4086. " <td>77</td>\n",
  4087. " </tr>\n",
  4088. " <tr>\n",
  4089. " <th>100915</th>\n",
  4090. " <td>plouftr01</td>\n",
  4091. " <td>152</td>\n",
  4092. " <td>573</td>\n",
  4093. " <td>140</td>\n",
  4094. " <td>22</td>\n",
  4095. " <td>86</td>\n",
  4096. " </tr>\n",
  4097. " <tr>\n",
  4098. " <th>100488</th>\n",
  4099. " <td>hunteto01</td>\n",
  4100. " <td>139</td>\n",
  4101. " <td>521</td>\n",
  4102. " <td>125</td>\n",
  4103. " <td>22</td>\n",
  4104. " <td>81</td>\n",
  4105. " </tr>\n",
  4106. " <tr>\n",
  4107. " <th>101164</th>\n",
  4108. " <td>suzukku01</td>\n",
  4109. " <td>131</td>\n",
  4110. " <td>433</td>\n",
  4111. " <td>104</td>\n",
  4112. " <td>5</td>\n",
  4113. " <td>50</td>\n",
  4114. " </tr>\n",
  4115. " <tr>\n",
  4116. " <th>101067</th>\n",
  4117. " <td>sanomi01</td>\n",
  4118. " <td>80</td>\n",
  4119. " <td>279</td>\n",
  4120. " <td>75</td>\n",
  4121. " <td>18</td>\n",
  4122. " <td>52</td>\n",
  4123. " </tr>\n",
  4124. " <tr>\n",
  4125. " <th>100816</th>\n",
  4126. " <td>nunezed02</td>\n",
  4127. " <td>72</td>\n",
  4128. " <td>188</td>\n",
  4129. " <td>53</td>\n",
  4130. " <td>4</td>\n",
  4131. " <td>20</td>\n",
  4132. " </tr>\n",
  4133. " <tr>\n",
  4134. " <th>101189</th>\n",
  4135. " <td>thielca01</td>\n",
  4136. " <td>6</td>\n",
  4137. " <td>0</td>\n",
  4138. " <td>0</td>\n",
  4139. " <td>0</td>\n",
  4140. " <td>0</td>\n",
  4141. " </tr>\n",
  4142. " <tr>\n",
  4143. " <th>100917</th>\n",
  4144. " <td>polanjo01</td>\n",
  4145. " <td>4</td>\n",
  4146. " <td>10</td>\n",
  4147. " <td>3</td>\n",
  4148. " <td>0</td>\n",
  4149. " <td>1</td>\n",
  4150. " </tr>\n",
  4151. " <tr>\n",
  4152. " <th>99954</th>\n",
  4153. " <td>bernido01</td>\n",
  4154. " <td>4</td>\n",
  4155. " <td>5</td>\n",
  4156. " <td>1</td>\n",
  4157. " <td>0</td>\n",
  4158. " <td>2</td>\n",
  4159. " </tr>\n",
  4160. " <tr>\n",
  4161. " <th>100564</th>\n",
  4162. " <td>keplema01</td>\n",
  4163. " <td>3</td>\n",
  4164. " <td>7</td>\n",
  4165. " <td>1</td>\n",
  4166. " <td>0</td>\n",
  4167. " <td>0</td>\n",
  4168. " </tr>\n",
  4169. " <tr>\n",
  4170. " <th>100729</th>\n",
  4171. " <td>meyeral01</td>\n",
  4172. " <td>2</td>\n",
  4173. " <td>0</td>\n",
  4174. " <td>0</td>\n",
  4175. " <td>0</td>\n",
  4176. " <td>0</td>\n",
  4177. " </tr>\n",
  4178. " </tbody>\n",
  4179. "</table>\n",
  4180. "</div>"
  4181. ],
  4182. "text/plain": [
  4183. " playerID G AB H HR RBI\n",
  4184. "100696 mauerjo01 158 592 157 10 66\n",
  4185. "100215 doziebr01 157 628 148 28 77\n",
  4186. "100915 plouftr01 152 573 140 22 86\n",
  4187. "100488 hunteto01 139 521 125 22 81\n",
  4188. "101164 suzukku01 131 433 104 5 50\n",
  4189. "101067 sanomi01 80 279 75 18 52\n",
  4190. "100816 nunezed02 72 188 53 4 20\n",
  4191. "101189 thielca01 6 0 0 0 0\n",
  4192. "100917 polanjo01 4 10 3 0 1\n",
  4193. "99954 bernido01 4 5 1 0 2\n",
  4194. "100564 keplema01 3 7 1 0 0\n",
  4195. "100729 meyeral01 2 0 0 0 0"
  4196. ]
  4197. },
  4198. "execution_count": 25,
  4199. "metadata": {},
  4200. "output_type": "execute_result"
  4201. }
  4202. ],
  4203. "source": [
  4204. "df_min_2015[:5].append([df_min_2015[10:12], df_min_2015[-5:]])"
  4205. ]
  4206. },
  4207. {
  4208. "cell_type": "markdown",
  4209. "metadata": {},
  4210. "source": [
  4211. "The same result can be achieved with [`pd.concat()`](http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.concat.html#pandas.concat), where the defaut `axis` is `0`."
  4212. ]
  4213. },
  4214. {
  4215. "cell_type": "code",
  4216. "execution_count": 26,
  4217. "metadata": {},
  4218. "outputs": [
  4219. {
  4220. "data": {
  4221. "text/html": [
  4222. "<div>\n",
  4223. "<style>\n",
  4224. " .dataframe thead tr:only-child th {\n",
  4225. " text-align: right;\n",
  4226. " }\n",
  4227. "\n",
  4228. " .dataframe thead th {\n",
  4229. " text-align: left;\n",
  4230. " }\n",
  4231. "\n",
  4232. " .dataframe tbody tr th {\n",
  4233. " vertical-align: top;\n",
  4234. " }\n",
  4235. "</style>\n",
  4236. "<table border=\"1\" class=\"dataframe\">\n",
  4237. " <thead>\n",
  4238. " <tr style=\"text-align: right;\">\n",
  4239. " <th></th>\n",
  4240. " <th>playerID</th>\n",
  4241. " <th>G</th>\n",
  4242. " <th>AB</th>\n",
  4243. " <th>H</th>\n",
  4244. " <th>HR</th>\n",
  4245. " <th>RBI</th>\n",
  4246. " </tr>\n",
  4247. " </thead>\n",
  4248. " <tbody>\n",
  4249. " <tr>\n",
  4250. " <th>100696</th>\n",
  4251. " <td>mauerjo01</td>\n",
  4252. " <td>158</td>\n",
  4253. " <td>592</td>\n",
  4254. " <td>157</td>\n",
  4255. " <td>10</td>\n",
  4256. " <td>66</td>\n",
  4257. " </tr>\n",
  4258. " <tr>\n",
  4259. " <th>100215</th>\n",
  4260. " <td>doziebr01</td>\n",
  4261. " <td>157</td>\n",
  4262. " <td>628</td>\n",
  4263. " <td>148</td>\n",
  4264. " <td>28</td>\n",
  4265. " <td>77</td>\n",
  4266. " </tr>\n",
  4267. " <tr>\n",
  4268. " <th>100915</th>\n",
  4269. " <td>plouftr01</td>\n",
  4270. " <td>152</td>\n",
  4271. " <td>573</td>\n",
  4272. " <td>140</td>\n",
  4273. " <td>22</td>\n",
  4274. " <td>86</td>\n",
  4275. " </tr>\n",
  4276. " <tr>\n",
  4277. " <th>100488</th>\n",
  4278. " <td>hunteto01</td>\n",
  4279. " <td>139</td>\n",
  4280. " <td>521</td>\n",
  4281. " <td>125</td>\n",
  4282. " <td>22</td>\n",
  4283. " <td>81</td>\n",
  4284. " </tr>\n",
  4285. " <tr>\n",
  4286. " <th>101164</th>\n",
  4287. " <td>suzukku01</td>\n",
  4288. " <td>131</td>\n",
  4289. " <td>433</td>\n",
  4290. " <td>104</td>\n",
  4291. " <td>5</td>\n",
  4292. " <td>50</td>\n",
  4293. " </tr>\n",
  4294. " <tr>\n",
  4295. " <th>101189</th>\n",
  4296. " <td>thielca01</td>\n",
  4297. " <td>6</td>\n",
  4298. " <td>0</td>\n",
  4299. " <td>0</td>\n",
  4300. " <td>0</td>\n",
  4301. " <td>0</td>\n",
  4302. " </tr>\n",
  4303. " <tr>\n",
  4304. " <th>100917</th>\n",
  4305. " <td>polanjo01</td>\n",
  4306. " <td>4</td>\n",
  4307. " <td>10</td>\n",
  4308. " <td>3</td>\n",
  4309. " <td>0</td>\n",
  4310. " <td>1</td>\n",
  4311. " </tr>\n",
  4312. " <tr>\n",
  4313. " <th>99954</th>\n",
  4314. " <td>bernido01</td>\n",
  4315. " <td>4</td>\n",
  4316. " <td>5</td>\n",
  4317. " <td>1</td>\n",
  4318. " <td>0</td>\n",
  4319. " <td>2</td>\n",
  4320. " </tr>\n",
  4321. " <tr>\n",
  4322. " <th>100564</th>\n",
  4323. " <td>keplema01</td>\n",
  4324. " <td>3</td>\n",
  4325. " <td>7</td>\n",
  4326. " <td>1</td>\n",
  4327. " <td>0</td>\n",
  4328. " <td>0</td>\n",
  4329. " </tr>\n",
  4330. " <tr>\n",
  4331. " <th>100729</th>\n",
  4332. " <td>meyeral01</td>\n",
  4333. " <td>2</td>\n",
  4334. " <td>0</td>\n",
  4335. " <td>0</td>\n",
  4336. " <td>0</td>\n",
  4337. " <td>0</td>\n",
  4338. " </tr>\n",
  4339. " </tbody>\n",
  4340. "</table>\n",
  4341. "</div>"
  4342. ],
  4343. "text/plain": [
  4344. " playerID G AB H HR RBI\n",
  4345. "100696 mauerjo01 158 592 157 10 66\n",
  4346. "100215 doziebr01 157 628 148 28 77\n",
  4347. "100915 plouftr01 152 573 140 22 86\n",
  4348. "100488 hunteto01 139 521 125 22 81\n",
  4349. "101164 suzukku01 131 433 104 5 50\n",
  4350. "101189 thielca01 6 0 0 0 0\n",
  4351. "100917 polanjo01 4 10 3 0 1\n",
  4352. "99954 bernido01 4 5 1 0 2\n",
  4353. "100564 keplema01 3 7 1 0 0\n",
  4354. "100729 meyeral01 2 0 0 0 0"
  4355. ]
  4356. },
  4357. "execution_count": 26,
  4358. "metadata": {},
  4359. "output_type": "execute_result"
  4360. }
  4361. ],
  4362. "source": [
  4363. "pd.concat([df_min_2015[:5], \n",
  4364. " df_min_2015[-5:]], axis=0)"
  4365. ]
  4366. },
  4367. {
  4368. "cell_type": "markdown",
  4369. "metadata": {},
  4370. "source": [
  4371. "But we can use `concat()` to make a _column-wise_ concatenation using `axis=1` (columns). "
  4372. ]
  4373. },
  4374. {
  4375. "cell_type": "code",
  4376. "execution_count": 27,
  4377. "metadata": {},
  4378. "outputs": [
  4379. {
  4380. "data": {
  4381. "text/html": [
  4382. "<div>\n",
  4383. "<style>\n",
  4384. " .dataframe thead tr:only-child th {\n",
  4385. " text-align: right;\n",
  4386. " }\n",
  4387. "\n",
  4388. " .dataframe thead th {\n",
  4389. " text-align: left;\n",
  4390. " }\n",
  4391. "\n",
  4392. " .dataframe tbody tr th {\n",
  4393. " vertical-align: top;\n",
  4394. " }\n",
  4395. "</style>\n",
  4396. "<table border=\"1\" class=\"dataframe\">\n",
  4397. " <thead>\n",
  4398. " <tr style=\"text-align: right;\">\n",
  4399. " <th></th>\n",
  4400. " <th>playerID</th>\n",
  4401. " <th>G</th>\n",
  4402. " <th>AB</th>\n",
  4403. " <th>H</th>\n",
  4404. " <th>HR</th>\n",
  4405. " <th>RBI</th>\n",
  4406. " <th>playerID</th>\n",
  4407. " <th>G</th>\n",
  4408. " <th>AB</th>\n",
  4409. " <th>H</th>\n",
  4410. " <th>HR</th>\n",
  4411. " <th>RBI</th>\n",
  4412. " </tr>\n",
  4413. " </thead>\n",
  4414. " <tbody>\n",
  4415. " <tr>\n",
  4416. " <th>99954</th>\n",
  4417. " <td>NaN</td>\n",
  4418. " <td>NaN</td>\n",
  4419. " <td>NaN</td>\n",
  4420. " <td>NaN</td>\n",
  4421. " <td>NaN</td>\n",
  4422. " <td>NaN</td>\n",
  4423. " <td>bernido01</td>\n",
  4424. " <td>4</td>\n",
  4425. " <td>5</td>\n",
  4426. " <td>1</td>\n",
  4427. " <td>0</td>\n",
  4428. " <td>2</td>\n",
  4429. " </tr>\n",
  4430. " <tr>\n",
  4431. " <th>100215</th>\n",
  4432. " <td>doziebr01</td>\n",
  4433. " <td>157</td>\n",
  4434. " <td>628</td>\n",
  4435. " <td>148</td>\n",
  4436. " <td>28</td>\n",
  4437. " <td>77</td>\n",
  4438. " <td>NaN</td>\n",
  4439. " <td>NaN</td>\n",
  4440. " <td>NaN</td>\n",
  4441. " <td>NaN</td>\n",
  4442. " <td>NaN</td>\n",
  4443. " <td>NaN</td>\n",
  4444. " </tr>\n",
  4445. " <tr>\n",
  4446. " <th>100488</th>\n",
  4447. " <td>hunteto01</td>\n",
  4448. " <td>139</td>\n",
  4449. " <td>521</td>\n",
  4450. " <td>125</td>\n",
  4451. " <td>22</td>\n",
  4452. " <td>81</td>\n",
  4453. " <td>NaN</td>\n",
  4454. " <td>NaN</td>\n",
  4455. " <td>NaN</td>\n",
  4456. " <td>NaN</td>\n",
  4457. " <td>NaN</td>\n",
  4458. " <td>NaN</td>\n",
  4459. " </tr>\n",
  4460. " <tr>\n",
  4461. " <th>100564</th>\n",
  4462. " <td>NaN</td>\n",
  4463. " <td>NaN</td>\n",
  4464. " <td>NaN</td>\n",
  4465. " <td>NaN</td>\n",
  4466. " <td>NaN</td>\n",
  4467. " <td>NaN</td>\n",
  4468. " <td>keplema01</td>\n",
  4469. " <td>3</td>\n",
  4470. " <td>7</td>\n",
  4471. " <td>1</td>\n",
  4472. " <td>0</td>\n",
  4473. " <td>0</td>\n",
  4474. " </tr>\n",
  4475. " <tr>\n",
  4476. " <th>100696</th>\n",
  4477. " <td>mauerjo01</td>\n",
  4478. " <td>158</td>\n",
  4479. " <td>592</td>\n",
  4480. " <td>157</td>\n",
  4481. " <td>10</td>\n",
  4482. " <td>66</td>\n",
  4483. " <td>NaN</td>\n",
  4484. " <td>NaN</td>\n",
  4485. " <td>NaN</td>\n",
  4486. " <td>NaN</td>\n",
  4487. " <td>NaN</td>\n",
  4488. " <td>NaN</td>\n",
  4489. " </tr>\n",
  4490. " <tr>\n",
  4491. " <th>100729</th>\n",
  4492. " <td>NaN</td>\n",
  4493. " <td>NaN</td>\n",
  4494. " <td>NaN</td>\n",
  4495. " <td>NaN</td>\n",
  4496. " <td>NaN</td>\n",
  4497. " <td>NaN</td>\n",
  4498. " <td>meyeral01</td>\n",
  4499. " <td>2</td>\n",
  4500. " <td>0</td>\n",
  4501. " <td>0</td>\n",
  4502. " <td>0</td>\n",
  4503. " <td>0</td>\n",
  4504. " </tr>\n",
  4505. " <tr>\n",
  4506. " <th>100915</th>\n",
  4507. " <td>plouftr01</td>\n",
  4508. " <td>152</td>\n",
  4509. " <td>573</td>\n",
  4510. " <td>140</td>\n",
  4511. " <td>22</td>\n",
  4512. " <td>86</td>\n",
  4513. " <td>NaN</td>\n",
  4514. " <td>NaN</td>\n",
  4515. " <td>NaN</td>\n",
  4516. " <td>NaN</td>\n",
  4517. " <td>NaN</td>\n",
  4518. " <td>NaN</td>\n",
  4519. " </tr>\n",
  4520. " <tr>\n",
  4521. " <th>100917</th>\n",
  4522. " <td>NaN</td>\n",
  4523. " <td>NaN</td>\n",
  4524. " <td>NaN</td>\n",
  4525. " <td>NaN</td>\n",
  4526. " <td>NaN</td>\n",
  4527. " <td>NaN</td>\n",
  4528. " <td>polanjo01</td>\n",
  4529. " <td>4</td>\n",
  4530. " <td>10</td>\n",
  4531. " <td>3</td>\n",
  4532. " <td>0</td>\n",
  4533. " <td>1</td>\n",
  4534. " </tr>\n",
  4535. " <tr>\n",
  4536. " <th>101164</th>\n",
  4537. " <td>suzukku01</td>\n",
  4538. " <td>131</td>\n",
  4539. " <td>433</td>\n",
  4540. " <td>104</td>\n",
  4541. " <td>5</td>\n",
  4542. " <td>50</td>\n",
  4543. " <td>NaN</td>\n",
  4544. " <td>NaN</td>\n",
  4545. " <td>NaN</td>\n",
  4546. " <td>NaN</td>\n",
  4547. " <td>NaN</td>\n",
  4548. " <td>NaN</td>\n",
  4549. " </tr>\n",
  4550. " <tr>\n",
  4551. " <th>101189</th>\n",
  4552. " <td>NaN</td>\n",
  4553. " <td>NaN</td>\n",
  4554. " <td>NaN</td>\n",
  4555. " <td>NaN</td>\n",
  4556. " <td>NaN</td>\n",
  4557. " <td>NaN</td>\n",
  4558. " <td>thielca01</td>\n",
  4559. " <td>6</td>\n",
  4560. " <td>0</td>\n",
  4561. " <td>0</td>\n",
  4562. " <td>0</td>\n",
  4563. " <td>0</td>\n",
  4564. " </tr>\n",
  4565. " </tbody>\n",
  4566. "</table>\n",
  4567. "</div>"
  4568. ],
  4569. "text/plain": [
  4570. " playerID G AB H HR RBI playerID G AB H HR RBI\n",
  4571. "99954 NaN NaN NaN NaN NaN NaN bernido01 4 5 1 0 2\n",
  4572. "100215 doziebr01 157 628 148 28 77 NaN NaN NaN NaN NaN NaN\n",
  4573. "100488 hunteto01 139 521 125 22 81 NaN NaN NaN NaN NaN NaN\n",
  4574. "100564 NaN NaN NaN NaN NaN NaN keplema01 3 7 1 0 0\n",
  4575. "100696 mauerjo01 158 592 157 10 66 NaN NaN NaN NaN NaN NaN\n",
  4576. "100729 NaN NaN NaN NaN NaN NaN meyeral01 2 0 0 0 0\n",
  4577. "100915 plouftr01 152 573 140 22 86 NaN NaN NaN NaN NaN NaN\n",
  4578. "100917 NaN NaN NaN NaN NaN NaN polanjo01 4 10 3 0 1\n",
  4579. "101164 suzukku01 131 433 104 5 50 NaN NaN NaN NaN NaN NaN\n",
  4580. "101189 NaN NaN NaN NaN NaN NaN thielca01 6 0 0 0 0"
  4581. ]
  4582. },
  4583. "execution_count": 27,
  4584. "metadata": {},
  4585. "output_type": "execute_result"
  4586. }
  4587. ],
  4588. "source": [
  4589. "pd.concat([df_min_2015[:5], \n",
  4590. " df_min_2015[-5:]], axis=1)"
  4591. ]
  4592. },
  4593. {
  4594. "cell_type": "markdown",
  4595. "metadata": {},
  4596. "source": [
  4597. "We can see that the indices are being considered in the concatenation and row indices are being joined. This behavior can be controlled via the `join` parameter, which we'll leave [for the reader to explore](http://pandas.pydata.org/pandas-docs/version/0.17.0/merging.html#concatenating-objects).\n",
  4598. "\n",
  4599. "One last thing we might want to do in an operation like this is to reset the index. To do so, we might start with ignoring the column index using the `ignore_index=True` so we can set it later to something more appropriate after the concatenation."
  4600. ]
  4601. },
  4602. {
  4603. "cell_type": "code",
  4604. "execution_count": 28,
  4605. "metadata": {},
  4606. "outputs": [
  4607. {
  4608. "data": {
  4609. "text/html": [
  4610. "<div>\n",
  4611. "<style>\n",
  4612. " .dataframe thead tr:only-child th {\n",
  4613. " text-align: right;\n",
  4614. " }\n",
  4615. "\n",
  4616. " .dataframe thead th {\n",
  4617. " text-align: left;\n",
  4618. " }\n",
  4619. "\n",
  4620. " .dataframe tbody tr th {\n",
  4621. " vertical-align: top;\n",
  4622. " }\n",
  4623. "</style>\n",
  4624. "<table border=\"1\" class=\"dataframe\">\n",
  4625. " <thead>\n",
  4626. " <tr style=\"text-align: right;\">\n",
  4627. " <th></th>\n",
  4628. " <th>0</th>\n",
  4629. " <th>1</th>\n",
  4630. " <th>2</th>\n",
  4631. " <th>3</th>\n",
  4632. " <th>4</th>\n",
  4633. " <th>5</th>\n",
  4634. " <th>6</th>\n",
  4635. " <th>7</th>\n",
  4636. " <th>8</th>\n",
  4637. " <th>9</th>\n",
  4638. " <th>10</th>\n",
  4639. " <th>11</th>\n",
  4640. " </tr>\n",
  4641. " </thead>\n",
  4642. " <tbody>\n",
  4643. " <tr>\n",
  4644. " <th>99954</th>\n",
  4645. " <td>NaN</td>\n",
  4646. " <td>NaN</td>\n",
  4647. " <td>NaN</td>\n",
  4648. " <td>NaN</td>\n",
  4649. " <td>NaN</td>\n",
  4650. " <td>NaN</td>\n",
  4651. " <td>bernido01</td>\n",
  4652. " <td>4</td>\n",
  4653. " <td>5</td>\n",
  4654. " <td>1</td>\n",
  4655. " <td>0</td>\n",
  4656. " <td>2</td>\n",
  4657. " </tr>\n",
  4658. " <tr>\n",
  4659. " <th>100215</th>\n",
  4660. " <td>doziebr01</td>\n",
  4661. " <td>157</td>\n",
  4662. " <td>628</td>\n",
  4663. " <td>148</td>\n",
  4664. " <td>28</td>\n",
  4665. " <td>77</td>\n",
  4666. " <td>NaN</td>\n",
  4667. " <td>NaN</td>\n",
  4668. " <td>NaN</td>\n",
  4669. " <td>NaN</td>\n",
  4670. " <td>NaN</td>\n",
  4671. " <td>NaN</td>\n",
  4672. " </tr>\n",
  4673. " <tr>\n",
  4674. " <th>100488</th>\n",
  4675. " <td>hunteto01</td>\n",
  4676. " <td>139</td>\n",
  4677. " <td>521</td>\n",
  4678. " <td>125</td>\n",
  4679. " <td>22</td>\n",
  4680. " <td>81</td>\n",
  4681. " <td>NaN</td>\n",
  4682. " <td>NaN</td>\n",
  4683. " <td>NaN</td>\n",
  4684. " <td>NaN</td>\n",
  4685. " <td>NaN</td>\n",
  4686. " <td>NaN</td>\n",
  4687. " </tr>\n",
  4688. " <tr>\n",
  4689. " <th>100564</th>\n",
  4690. " <td>NaN</td>\n",
  4691. " <td>NaN</td>\n",
  4692. " <td>NaN</td>\n",
  4693. " <td>NaN</td>\n",
  4694. " <td>NaN</td>\n",
  4695. " <td>NaN</td>\n",
  4696. " <td>keplema01</td>\n",
  4697. " <td>3</td>\n",
  4698. " <td>7</td>\n",
  4699. " <td>1</td>\n",
  4700. " <td>0</td>\n",
  4701. " <td>0</td>\n",
  4702. " </tr>\n",
  4703. " <tr>\n",
  4704. " <th>100696</th>\n",
  4705. " <td>mauerjo01</td>\n",
  4706. " <td>158</td>\n",
  4707. " <td>592</td>\n",
  4708. " <td>157</td>\n",
  4709. " <td>10</td>\n",
  4710. " <td>66</td>\n",
  4711. " <td>NaN</td>\n",
  4712. " <td>NaN</td>\n",
  4713. " <td>NaN</td>\n",
  4714. " <td>NaN</td>\n",
  4715. " <td>NaN</td>\n",
  4716. " <td>NaN</td>\n",
  4717. " </tr>\n",
  4718. " <tr>\n",
  4719. " <th>100729</th>\n",
  4720. " <td>NaN</td>\n",
  4721. " <td>NaN</td>\n",
  4722. " <td>NaN</td>\n",
  4723. " <td>NaN</td>\n",
  4724. " <td>NaN</td>\n",
  4725. " <td>NaN</td>\n",
  4726. " <td>meyeral01</td>\n",
  4727. " <td>2</td>\n",
  4728. " <td>0</td>\n",
  4729. " <td>0</td>\n",
  4730. " <td>0</td>\n",
  4731. " <td>0</td>\n",
  4732. " </tr>\n",
  4733. " <tr>\n",
  4734. " <th>100915</th>\n",
  4735. " <td>plouftr01</td>\n",
  4736. " <td>152</td>\n",
  4737. " <td>573</td>\n",
  4738. " <td>140</td>\n",
  4739. " <td>22</td>\n",
  4740. " <td>86</td>\n",
  4741. " <td>NaN</td>\n",
  4742. " <td>NaN</td>\n",
  4743. " <td>NaN</td>\n",
  4744. " <td>NaN</td>\n",
  4745. " <td>NaN</td>\n",
  4746. " <td>NaN</td>\n",
  4747. " </tr>\n",
  4748. " <tr>\n",
  4749. " <th>100917</th>\n",
  4750. " <td>NaN</td>\n",
  4751. " <td>NaN</td>\n",
  4752. " <td>NaN</td>\n",
  4753. " <td>NaN</td>\n",
  4754. " <td>NaN</td>\n",
  4755. " <td>NaN</td>\n",
  4756. " <td>polanjo01</td>\n",
  4757. " <td>4</td>\n",
  4758. " <td>10</td>\n",
  4759. " <td>3</td>\n",
  4760. " <td>0</td>\n",
  4761. " <td>1</td>\n",
  4762. " </tr>\n",
  4763. " <tr>\n",
  4764. " <th>101164</th>\n",
  4765. " <td>suzukku01</td>\n",
  4766. " <td>131</td>\n",
  4767. " <td>433</td>\n",
  4768. " <td>104</td>\n",
  4769. " <td>5</td>\n",
  4770. " <td>50</td>\n",
  4771. " <td>NaN</td>\n",
  4772. " <td>NaN</td>\n",
  4773. " <td>NaN</td>\n",
  4774. " <td>NaN</td>\n",
  4775. " <td>NaN</td>\n",
  4776. " <td>NaN</td>\n",
  4777. " </tr>\n",
  4778. " <tr>\n",
  4779. " <th>101189</th>\n",
  4780. " <td>NaN</td>\n",
  4781. " <td>NaN</td>\n",
  4782. " <td>NaN</td>\n",
  4783. " <td>NaN</td>\n",
  4784. " <td>NaN</td>\n",
  4785. " <td>NaN</td>\n",
  4786. " <td>thielca01</td>\n",
  4787. " <td>6</td>\n",
  4788. " <td>0</td>\n",
  4789. " <td>0</td>\n",
  4790. " <td>0</td>\n",
  4791. " <td>0</td>\n",
  4792. " </tr>\n",
  4793. " </tbody>\n",
  4794. "</table>\n",
  4795. "</div>"
  4796. ],
  4797. "text/plain": [
  4798. " 0 1 2 3 4 5 6 7 8 9 10 11\n",
  4799. "99954 NaN NaN NaN NaN NaN NaN bernido01 4 5 1 0 2\n",
  4800. "100215 doziebr01 157 628 148 28 77 NaN NaN NaN NaN NaN NaN\n",
  4801. "100488 hunteto01 139 521 125 22 81 NaN NaN NaN NaN NaN NaN\n",
  4802. "100564 NaN NaN NaN NaN NaN NaN keplema01 3 7 1 0 0\n",
  4803. "100696 mauerjo01 158 592 157 10 66 NaN NaN NaN NaN NaN NaN\n",
  4804. "100729 NaN NaN NaN NaN NaN NaN meyeral01 2 0 0 0 0\n",
  4805. "100915 plouftr01 152 573 140 22 86 NaN NaN NaN NaN NaN NaN\n",
  4806. "100917 NaN NaN NaN NaN NaN NaN polanjo01 4 10 3 0 1\n",
  4807. "101164 suzukku01 131 433 104 5 50 NaN NaN NaN NaN NaN NaN\n",
  4808. "101189 NaN NaN NaN NaN NaN NaN thielca01 6 0 0 0 0"
  4809. ]
  4810. },
  4811. "execution_count": 28,
  4812. "metadata": {},
  4813. "output_type": "execute_result"
  4814. }
  4815. ],
  4816. "source": [
  4817. "pd.concat([df_min_2015[:5], \n",
  4818. " df_min_2015[-5:]], axis=1, ignore_index=True)"
  4819. ]
  4820. },
  4821. {
  4822. "cell_type": "markdown",
  4823. "metadata": {},
  4824. "source": [
  4825. "## Advanced indexing\n",
  4826. "Pandas provides the ability to build more complex indices allowing for highly flexible and natural data access.\n",
  4827. "\n",
  4828. "We will cover the basics of through the [`MultiIndex`](http://pandas.pydata.org/pandas-docs/version/0.17.0/advanced.html#hierarchical-indexing-multiindex) object and will the the remaining exploration to the reader."
  4829. ]
  4830. },
  4831. {
  4832. "cell_type": "markdown",
  4833. "metadata": {},
  4834. "source": [
  4835. "Let's get the players on the Washington Nationals who played 100 or more games in 2015 and 2016."
  4836. ]
  4837. },
  4838. {
  4839. "cell_type": "code",
  4840. "execution_count": 29,
  4841. "metadata": {
  4842. "collapsed": true
  4843. },
  4844. "outputs": [],
  4845. "source": [
  4846. "df_was = df[(df.yearID > 2014) & (df.teamID=='WAS') & (df.G > 99)]"
  4847. ]
  4848. },
  4849. {
  4850. "cell_type": "code",
  4851. "execution_count": 30,
  4852. "metadata": {},
  4853. "outputs": [
  4854. {
  4855. "data": {
  4856. "text/html": [
  4857. "<div>\n",
  4858. "<style>\n",
  4859. " .dataframe thead tr:only-child th {\n",
  4860. " text-align: right;\n",
  4861. " }\n",
  4862. "\n",
  4863. " .dataframe thead th {\n",
  4864. " text-align: left;\n",
  4865. " }\n",
  4866. "\n",
  4867. " .dataframe tbody tr th {\n",
  4868. " vertical-align: top;\n",
  4869. " }\n",
  4870. "</style>\n",
  4871. "<table border=\"1\" class=\"dataframe\">\n",
  4872. " <thead>\n",
  4873. " <tr style=\"text-align: right;\">\n",
  4874. " <th></th>\n",
  4875. " <th>playerID</th>\n",
  4876. " <th>yearID</th>\n",
  4877. " <th>stint</th>\n",
  4878. " <th>teamID</th>\n",
  4879. " <th>lgID</th>\n",
  4880. " <th>G</th>\n",
  4881. " <th>AB</th>\n",
  4882. " <th>R</th>\n",
  4883. " <th>H</th>\n",
  4884. " <th>2B</th>\n",
  4885. " <th>...</th>\n",
  4886. " <th>RBI</th>\n",
  4887. " <th>SB</th>\n",
  4888. " <th>CS</th>\n",
  4889. " <th>BB</th>\n",
  4890. " <th>SO</th>\n",
  4891. " <th>IBB</th>\n",
  4892. " <th>HBP</th>\n",
  4893. " <th>SH</th>\n",
  4894. " <th>SF</th>\n",
  4895. " <th>GIDP</th>\n",
  4896. " </tr>\n",
  4897. " </thead>\n",
  4898. " <tbody>\n",
  4899. " <tr>\n",
  4900. " <th>100193</th>\n",
  4901. " <td>desmoia01</td>\n",
  4902. " <td>2015</td>\n",
  4903. " <td>1</td>\n",
  4904. " <td>WAS</td>\n",
  4905. " <td>NL</td>\n",
  4906. " <td>156</td>\n",
  4907. " <td>583</td>\n",
  4908. " <td>69</td>\n",
  4909. " <td>136</td>\n",
  4910. " <td>27</td>\n",
  4911. " <td>...</td>\n",
  4912. " <td>62.0</td>\n",
  4913. " <td>13.0</td>\n",
  4914. " <td>5.0</td>\n",
  4915. " <td>45</td>\n",
  4916. " <td>187.0</td>\n",
  4917. " <td>0.0</td>\n",
  4918. " <td>3.0</td>\n",
  4919. " <td>6.0</td>\n",
  4920. " <td>4.0</td>\n",
  4921. " <td>9.0</td>\n",
  4922. " </tr>\n",
  4923. " <tr>\n",
  4924. " <th>100250</th>\n",
  4925. " <td>escobyu01</td>\n",
  4926. " <td>2015</td>\n",
  4927. " <td>1</td>\n",
  4928. " <td>WAS</td>\n",
  4929. " <td>NL</td>\n",
  4930. " <td>139</td>\n",
  4931. " <td>535</td>\n",
  4932. " <td>75</td>\n",
  4933. " <td>168</td>\n",
  4934. " <td>25</td>\n",
  4935. " <td>...</td>\n",
  4936. " <td>56.0</td>\n",
  4937. " <td>2.0</td>\n",
  4938. " <td>2.0</td>\n",
  4939. " <td>45</td>\n",
  4940. " <td>70.0</td>\n",
  4941. " <td>0.0</td>\n",
  4942. " <td>8.0</td>\n",
  4943. " <td>1.0</td>\n",
  4944. " <td>2.0</td>\n",
  4945. " <td>24.0</td>\n",
  4946. " </tr>\n",
  4947. " <tr>\n",
  4948. " <th>100251</th>\n",
  4949. " <td>espinda01</td>\n",
  4950. " <td>2015</td>\n",
  4951. " <td>1</td>\n",
  4952. " <td>WAS</td>\n",
  4953. " <td>NL</td>\n",
  4954. " <td>118</td>\n",
  4955. " <td>367</td>\n",
  4956. " <td>59</td>\n",
  4957. " <td>88</td>\n",
  4958. " <td>21</td>\n",
  4959. " <td>...</td>\n",
  4960. " <td>37.0</td>\n",
  4961. " <td>5.0</td>\n",
  4962. " <td>2.0</td>\n",
  4963. " <td>33</td>\n",
  4964. " <td>106.0</td>\n",
  4965. " <td>5.0</td>\n",
  4966. " <td>6.0</td>\n",
  4967. " <td>3.0</td>\n",
  4968. " <td>3.0</td>\n",
  4969. " <td>6.0</td>\n",
  4970. " </tr>\n",
  4971. " <tr>\n",
  4972. " <th>100422</th>\n",
  4973. " <td>harpebr03</td>\n",
  4974. " <td>2015</td>\n",
  4975. " <td>1</td>\n",
  4976. " <td>WAS</td>\n",
  4977. " <td>NL</td>\n",
  4978. " <td>153</td>\n",
  4979. " <td>521</td>\n",
  4980. " <td>118</td>\n",
  4981. " <td>172</td>\n",
  4982. " <td>38</td>\n",
  4983. " <td>...</td>\n",
  4984. " <td>99.0</td>\n",
  4985. " <td>6.0</td>\n",
  4986. " <td>4.0</td>\n",
  4987. " <td>124</td>\n",
  4988. " <td>131.0</td>\n",
  4989. " <td>15.0</td>\n",
  4990. " <td>5.0</td>\n",
  4991. " <td>0.0</td>\n",
  4992. " <td>4.0</td>\n",
  4993. " <td>15.0</td>\n",
  4994. " </tr>\n",
  4995. " <tr>\n",
  4996. " <th>100950</th>\n",
  4997. " <td>ramoswi01</td>\n",
  4998. " <td>2015</td>\n",
  4999. " <td>1</td>\n",
  5000. " <td>WAS</td>\n",
  5001. " <td>NL</td>\n",
  5002. " <td>128</td>\n",
  5003. " <td>475</td>\n",
  5004. " <td>41</td>\n",
  5005. " <td>109</td>\n",
  5006. " <td>16</td>\n",
  5007. " <td>...</td>\n",
  5008. " <td>68.0</td>\n",
  5009. " <td>0.0</td>\n",
  5010. " <td>0.0</td>\n",
  5011. " <td>21</td>\n",
  5012. " <td>101.0</td>\n",
  5013. " <td>2.0</td>\n",
  5014. " <td>0.0</td>\n",
  5015. " <td>0.0</td>\n",
  5016. " <td>8.0</td>\n",
  5017. " <td>16.0</td>\n",
  5018. " </tr>\n",
  5019. " </tbody>\n",
  5020. "</table>\n",
  5021. "<p>5 rows × 22 columns</p>\n",
  5022. "</div>"
  5023. ],
  5024. "text/plain": [
  5025. " playerID yearID stint teamID lgID G AB R H 2B ... \\\n",
  5026. "100193 desmoia01 2015 1 WAS NL 156 583 69 136 27 ... \n",
  5027. "100250 escobyu01 2015 1 WAS NL 139 535 75 168 25 ... \n",
  5028. "100251 espinda01 2015 1 WAS NL 118 367 59 88 21 ... \n",
  5029. "100422 harpebr03 2015 1 WAS NL 153 521 118 172 38 ... \n",
  5030. "100950 ramoswi01 2015 1 WAS NL 128 475 41 109 16 ... \n",
  5031. "\n",
  5032. " RBI SB CS BB SO IBB HBP SH SF GIDP \n",
  5033. "100193 62.0 13.0 5.0 45 187.0 0.0 3.0 6.0 4.0 9.0 \n",
  5034. "100250 56.0 2.0 2.0 45 70.0 0.0 8.0 1.0 2.0 24.0 \n",
  5035. "100251 37.0 5.0 2.0 33 106.0 5.0 6.0 3.0 3.0 6.0 \n",
  5036. "100422 99.0 6.0 4.0 124 131.0 15.0 5.0 0.0 4.0 15.0 \n",
  5037. "100950 68.0 0.0 0.0 21 101.0 2.0 0.0 0.0 8.0 16.0 \n",
  5038. "\n",
  5039. "[5 rows x 22 columns]"
  5040. ]
  5041. },
  5042. "execution_count": 30,
  5043. "metadata": {},
  5044. "output_type": "execute_result"
  5045. }
  5046. ],
  5047. "source": [
  5048. "df_was.head()"
  5049. ]
  5050. },
  5051. {
  5052. "cell_type": "markdown",
  5053. "metadata": {},
  5054. "source": [
  5055. "One obvious problem if we were to access the data here by player and year, we have to build a much more involved query and even more so if we needed to ignore data.\n",
  5056. "\n",
  5057. "We are going to create a _hierarchical index_ or _MultiIndex_ to solve this problem. We'll take take liberty to drop columns we don't need (`teamID`, `ldID`, `stint`) and reorganize the index hierarchically.\n",
  5058. "\n",
  5059. "We will use `MultiIndex` using a _tuple_ of the data we need and provide the index first by _player_, then by _year_. To do this we'll just grab all the player IDs and `zip` them with the year. This will look something like this:"
  5060. ]
  5061. },
  5062. {
  5063. "cell_type": "code",
  5064. "execution_count": 31,
  5065. "metadata": {},
  5066. "outputs": [
  5067. {
  5068. "data": {
  5069. "text/plain": [
  5070. "(('desmoia01', 2015),\n",
  5071. " ('escobyu01', 2015),\n",
  5072. " ('espinda01', 2015),\n",
  5073. " ('espinda01', 2016),\n",
  5074. " ('harpebr03', 2015),\n",
  5075. " ('harpebr03', 2016),\n",
  5076. " ('murphda08', 2016),\n",
  5077. " ('ramoswi01', 2015),\n",
  5078. " ('ramoswi01', 2016),\n",
  5079. " ('rendoan01', 2016),\n",
  5080. " ('reverbe01', 2016),\n",
  5081. " ('robincl01', 2015),\n",
  5082. " ('robincl01', 2016),\n",
  5083. " ('taylomi02', 2015),\n",
  5084. " ('werthja01', 2016),\n",
  5085. " ('zimmery01', 2016))"
  5086. ]
  5087. },
  5088. "execution_count": 31,
  5089. "metadata": {},
  5090. "output_type": "execute_result"
  5091. }
  5092. ],
  5093. "source": [
  5094. "tuple(\n",
  5095. "zip(\n",
  5096. " df_was[['playerID','yearID']].sort_values(by='playerID')['playerID'],\n",
  5097. " df_was[['playerID','yearID']].sort_values(by='playerID')['yearID']\n",
  5098. ")\n",
  5099. ")"
  5100. ]
  5101. },
  5102. {
  5103. "cell_type": "code",
  5104. "execution_count": 32,
  5105. "metadata": {},
  5106. "outputs": [
  5107. {
  5108. "data": {
  5109. "text/plain": [
  5110. "MultiIndex(levels=[['desmoia01', 'escobyu01', 'espinda01', 'harpebr03', 'murphda08', 'ramoswi01', 'rendoan01', 'reverbe01', 'robincl01', 'taylomi02', 'werthja01', 'zimmery01'], [2015, 2016]],\n",
  5111. " labels=[[0, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10, 11], [0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1]])"
  5112. ]
  5113. },
  5114. "execution_count": 32,
  5115. "metadata": {},
  5116. "output_type": "execute_result"
  5117. }
  5118. ],
  5119. "source": [
  5120. "# create an index to be used over the data we're interested in\n",
  5121. "idx = \\\n",
  5122. " pd.MultiIndex.from_tuples(\n",
  5123. " tuple(\n",
  5124. " zip(\n",
  5125. " df_was[['playerID','yearID']].sort_values(by='playerID')['playerID'],\n",
  5126. " df_was[['playerID','yearID']].sort_values(by='playerID')['yearID']))\n",
  5127. " )\n",
  5128. "idx"
  5129. ]
  5130. },
  5131. {
  5132. "cell_type": "markdown",
  5133. "metadata": {},
  5134. "source": [
  5135. "Notice now that we have two _levels_ in our _row axis_ (axis 0) and we will now use that index to build the hierachically indexed DataFrame."
  5136. ]
  5137. },
  5138. {
  5139. "cell_type": "code",
  5140. "execution_count": 33,
  5141. "metadata": {},
  5142. "outputs": [
  5143. {
  5144. "data": {
  5145. "text/html": [
  5146. "<div>\n",
  5147. "<style>\n",
  5148. " .dataframe thead tr:only-child th {\n",
  5149. " text-align: right;\n",
  5150. " }\n",
  5151. "\n",
  5152. " .dataframe thead th {\n",
  5153. " text-align: left;\n",
  5154. " }\n",
  5155. "\n",
  5156. " .dataframe tbody tr th {\n",
  5157. " vertical-align: top;\n",
  5158. " }\n",
  5159. "</style>\n",
  5160. "<table border=\"1\" class=\"dataframe\">\n",
  5161. " <thead>\n",
  5162. " <tr style=\"text-align: right;\">\n",
  5163. " <th></th>\n",
  5164. " <th></th>\n",
  5165. " <th>G</th>\n",
  5166. " <th>AB</th>\n",
  5167. " <th>R</th>\n",
  5168. " <th>H</th>\n",
  5169. " <th>2B</th>\n",
  5170. " <th>3B</th>\n",
  5171. " <th>HR</th>\n",
  5172. " <th>RBI</th>\n",
  5173. " <th>SB</th>\n",
  5174. " <th>CS</th>\n",
  5175. " <th>BB</th>\n",
  5176. " <th>SO</th>\n",
  5177. " <th>IBB</th>\n",
  5178. " <th>HBP</th>\n",
  5179. " <th>SH</th>\n",
  5180. " <th>SF</th>\n",
  5181. " <th>GIDP</th>\n",
  5182. " </tr>\n",
  5183. " </thead>\n",
  5184. " <tbody>\n",
  5185. " <tr>\n",
  5186. " <th>desmoia01</th>\n",
  5187. " <th>2015</th>\n",
  5188. " <td>156</td>\n",
  5189. " <td>583</td>\n",
  5190. " <td>69</td>\n",
  5191. " <td>136</td>\n",
  5192. " <td>27</td>\n",
  5193. " <td>2</td>\n",
  5194. " <td>19</td>\n",
  5195. " <td>62.0</td>\n",
  5196. " <td>13.0</td>\n",
  5197. " <td>5.0</td>\n",
  5198. " <td>45</td>\n",
  5199. " <td>187.0</td>\n",
  5200. " <td>0.0</td>\n",
  5201. " <td>3.0</td>\n",
  5202. " <td>6.0</td>\n",
  5203. " <td>4.0</td>\n",
  5204. " <td>9.0</td>\n",
  5205. " </tr>\n",
  5206. " <tr>\n",
  5207. " <th>escobyu01</th>\n",
  5208. " <th>2015</th>\n",
  5209. " <td>139</td>\n",
  5210. " <td>535</td>\n",
  5211. " <td>75</td>\n",
  5212. " <td>168</td>\n",
  5213. " <td>25</td>\n",
  5214. " <td>1</td>\n",
  5215. " <td>9</td>\n",
  5216. " <td>56.0</td>\n",
  5217. " <td>2.0</td>\n",
  5218. " <td>2.0</td>\n",
  5219. " <td>45</td>\n",
  5220. " <td>70.0</td>\n",
  5221. " <td>0.0</td>\n",
  5222. " <td>8.0</td>\n",
  5223. " <td>1.0</td>\n",
  5224. " <td>2.0</td>\n",
  5225. " <td>24.0</td>\n",
  5226. " </tr>\n",
  5227. " <tr>\n",
  5228. " <th rowspan=\"2\" valign=\"top\">espinda01</th>\n",
  5229. " <th>2015</th>\n",
  5230. " <td>118</td>\n",
  5231. " <td>367</td>\n",
  5232. " <td>59</td>\n",
  5233. " <td>88</td>\n",
  5234. " <td>21</td>\n",
  5235. " <td>1</td>\n",
  5236. " <td>13</td>\n",
  5237. " <td>37.0</td>\n",
  5238. " <td>5.0</td>\n",
  5239. " <td>2.0</td>\n",
  5240. " <td>33</td>\n",
  5241. " <td>106.0</td>\n",
  5242. " <td>5.0</td>\n",
  5243. " <td>6.0</td>\n",
  5244. " <td>3.0</td>\n",
  5245. " <td>3.0</td>\n",
  5246. " <td>6.0</td>\n",
  5247. " </tr>\n",
  5248. " <tr>\n",
  5249. " <th>2016</th>\n",
  5250. " <td>157</td>\n",
  5251. " <td>516</td>\n",
  5252. " <td>66</td>\n",
  5253. " <td>108</td>\n",
  5254. " <td>15</td>\n",
  5255. " <td>0</td>\n",
  5256. " <td>24</td>\n",
  5257. " <td>72.0</td>\n",
  5258. " <td>9.0</td>\n",
  5259. " <td>2.0</td>\n",
  5260. " <td>54</td>\n",
  5261. " <td>174.0</td>\n",
  5262. " <td>12.0</td>\n",
  5263. " <td>20.0</td>\n",
  5264. " <td>7.0</td>\n",
  5265. " <td>4.0</td>\n",
  5266. " <td>4.0</td>\n",
  5267. " </tr>\n",
  5268. " <tr>\n",
  5269. " <th rowspan=\"2\" valign=\"top\">harpebr03</th>\n",
  5270. " <th>2015</th>\n",
  5271. " <td>153</td>\n",
  5272. " <td>521</td>\n",
  5273. " <td>118</td>\n",
  5274. " <td>172</td>\n",
  5275. " <td>38</td>\n",
  5276. " <td>1</td>\n",
  5277. " <td>42</td>\n",
  5278. " <td>99.0</td>\n",
  5279. " <td>6.0</td>\n",
  5280. " <td>4.0</td>\n",
  5281. " <td>124</td>\n",
  5282. " <td>131.0</td>\n",
  5283. " <td>15.0</td>\n",
  5284. " <td>5.0</td>\n",
  5285. " <td>0.0</td>\n",
  5286. " <td>4.0</td>\n",
  5287. " <td>15.0</td>\n",
  5288. " </tr>\n",
  5289. " <tr>\n",
  5290. " <th>2016</th>\n",
  5291. " <td>147</td>\n",
  5292. " <td>506</td>\n",
  5293. " <td>84</td>\n",
  5294. " <td>123</td>\n",
  5295. " <td>24</td>\n",
  5296. " <td>2</td>\n",
  5297. " <td>24</td>\n",
  5298. " <td>86.0</td>\n",
  5299. " <td>21.0</td>\n",
  5300. " <td>10.0</td>\n",
  5301. " <td>108</td>\n",
  5302. " <td>117.0</td>\n",
  5303. " <td>20.0</td>\n",
  5304. " <td>3.0</td>\n",
  5305. " <td>0.0</td>\n",
  5306. " <td>10.0</td>\n",
  5307. " <td>11.0</td>\n",
  5308. " </tr>\n",
  5309. " <tr>\n",
  5310. " <th>murphda08</th>\n",
  5311. " <th>2016</th>\n",
  5312. " <td>142</td>\n",
  5313. " <td>531</td>\n",
  5314. " <td>88</td>\n",
  5315. " <td>184</td>\n",
  5316. " <td>47</td>\n",
  5317. " <td>5</td>\n",
  5318. " <td>25</td>\n",
  5319. " <td>104.0</td>\n",
  5320. " <td>5.0</td>\n",
  5321. " <td>3.0</td>\n",
  5322. " <td>35</td>\n",
  5323. " <td>57.0</td>\n",
  5324. " <td>10.0</td>\n",
  5325. " <td>8.0</td>\n",
  5326. " <td>0.0</td>\n",
  5327. " <td>8.0</td>\n",
  5328. " <td>4.0</td>\n",
  5329. " </tr>\n",
  5330. " <tr>\n",
  5331. " <th rowspan=\"2\" valign=\"top\">ramoswi01</th>\n",
  5332. " <th>2015</th>\n",
  5333. " <td>128</td>\n",
  5334. " <td>475</td>\n",
  5335. " <td>41</td>\n",
  5336. " <td>109</td>\n",
  5337. " <td>16</td>\n",
  5338. " <td>0</td>\n",
  5339. " <td>15</td>\n",
  5340. " <td>68.0</td>\n",
  5341. " <td>0.0</td>\n",
  5342. " <td>0.0</td>\n",
  5343. " <td>21</td>\n",
  5344. " <td>101.0</td>\n",
  5345. " <td>2.0</td>\n",
  5346. " <td>0.0</td>\n",
  5347. " <td>0.0</td>\n",
  5348. " <td>8.0</td>\n",
  5349. " <td>16.0</td>\n",
  5350. " </tr>\n",
  5351. " <tr>\n",
  5352. " <th>2016</th>\n",
  5353. " <td>131</td>\n",
  5354. " <td>482</td>\n",
  5355. " <td>58</td>\n",
  5356. " <td>148</td>\n",
  5357. " <td>25</td>\n",
  5358. " <td>0</td>\n",
  5359. " <td>22</td>\n",
  5360. " <td>80.0</td>\n",
  5361. " <td>0.0</td>\n",
  5362. " <td>0.0</td>\n",
  5363. " <td>35</td>\n",
  5364. " <td>79.0</td>\n",
  5365. " <td>2.0</td>\n",
  5366. " <td>2.0</td>\n",
  5367. " <td>0.0</td>\n",
  5368. " <td>4.0</td>\n",
  5369. " <td>17.0</td>\n",
  5370. " </tr>\n",
  5371. " <tr>\n",
  5372. " <th>rendoan01</th>\n",
  5373. " <th>2016</th>\n",
  5374. " <td>156</td>\n",
  5375. " <td>567</td>\n",
  5376. " <td>91</td>\n",
  5377. " <td>153</td>\n",
  5378. " <td>38</td>\n",
  5379. " <td>2</td>\n",
  5380. " <td>20</td>\n",
  5381. " <td>85.0</td>\n",
  5382. " <td>12.0</td>\n",
  5383. " <td>6.0</td>\n",
  5384. " <td>65</td>\n",
  5385. " <td>117.0</td>\n",
  5386. " <td>2.0</td>\n",
  5387. " <td>7.0</td>\n",
  5388. " <td>0.0</td>\n",
  5389. " <td>8.0</td>\n",
  5390. " <td>5.0</td>\n",
  5391. " </tr>\n",
  5392. " <tr>\n",
  5393. " <th>reverbe01</th>\n",
  5394. " <th>2016</th>\n",
  5395. " <td>103</td>\n",
  5396. " <td>350</td>\n",
  5397. " <td>44</td>\n",
  5398. " <td>76</td>\n",
  5399. " <td>9</td>\n",
  5400. " <td>7</td>\n",
  5401. " <td>2</td>\n",
  5402. " <td>24.0</td>\n",
  5403. " <td>14.0</td>\n",
  5404. " <td>5.0</td>\n",
  5405. " <td>18</td>\n",
  5406. " <td>34.0</td>\n",
  5407. " <td>0.0</td>\n",
  5408. " <td>3.0</td>\n",
  5409. " <td>2.0</td>\n",
  5410. " <td>2.0</td>\n",
  5411. " <td>12.0</td>\n",
  5412. " </tr>\n",
  5413. " <tr>\n",
  5414. " <th rowspan=\"2\" valign=\"top\">robincl01</th>\n",
  5415. " <th>2015</th>\n",
  5416. " <td>126</td>\n",
  5417. " <td>309</td>\n",
  5418. " <td>44</td>\n",
  5419. " <td>84</td>\n",
  5420. " <td>15</td>\n",
  5421. " <td>1</td>\n",
  5422. " <td>10</td>\n",
  5423. " <td>34.0</td>\n",
  5424. " <td>0.0</td>\n",
  5425. " <td>0.0</td>\n",
  5426. " <td>37</td>\n",
  5427. " <td>52.0</td>\n",
  5428. " <td>4.0</td>\n",
  5429. " <td>5.0</td>\n",
  5430. " <td>0.0</td>\n",
  5431. " <td>1.0</td>\n",
  5432. " <td>6.0</td>\n",
  5433. " </tr>\n",
  5434. " <tr>\n",
  5435. " <th>2016</th>\n",
  5436. " <td>104</td>\n",
  5437. " <td>196</td>\n",
  5438. " <td>16</td>\n",
  5439. " <td>46</td>\n",
  5440. " <td>4</td>\n",
  5441. " <td>0</td>\n",
  5442. " <td>5</td>\n",
  5443. " <td>26.0</td>\n",
  5444. " <td>0.0</td>\n",
  5445. " <td>0.0</td>\n",
  5446. " <td>20</td>\n",
  5447. " <td>38.0</td>\n",
  5448. " <td>0.0</td>\n",
  5449. " <td>2.0</td>\n",
  5450. " <td>1.0</td>\n",
  5451. " <td>5.0</td>\n",
  5452. " <td>4.0</td>\n",
  5453. " </tr>\n",
  5454. " <tr>\n",
  5455. " <th>taylomi02</th>\n",
  5456. " <th>2015</th>\n",
  5457. " <td>138</td>\n",
  5458. " <td>472</td>\n",
  5459. " <td>49</td>\n",
  5460. " <td>108</td>\n",
  5461. " <td>15</td>\n",
  5462. " <td>2</td>\n",
  5463. " <td>14</td>\n",
  5464. " <td>63.0</td>\n",
  5465. " <td>16.0</td>\n",
  5466. " <td>3.0</td>\n",
  5467. " <td>35</td>\n",
  5468. " <td>158.0</td>\n",
  5469. " <td>9.0</td>\n",
  5470. " <td>1.0</td>\n",
  5471. " <td>1.0</td>\n",
  5472. " <td>2.0</td>\n",
  5473. " <td>5.0</td>\n",
  5474. " </tr>\n",
  5475. " <tr>\n",
  5476. " <th>werthja01</th>\n",
  5477. " <th>2016</th>\n",
  5478. " <td>143</td>\n",
  5479. " <td>525</td>\n",
  5480. " <td>84</td>\n",
  5481. " <td>128</td>\n",
  5482. " <td>28</td>\n",
  5483. " <td>0</td>\n",
  5484. " <td>21</td>\n",
  5485. " <td>69.0</td>\n",
  5486. " <td>5.0</td>\n",
  5487. " <td>1.0</td>\n",
  5488. " <td>71</td>\n",
  5489. " <td>139.0</td>\n",
  5490. " <td>0.0</td>\n",
  5491. " <td>4.0</td>\n",
  5492. " <td>0.0</td>\n",
  5493. " <td>6.0</td>\n",
  5494. " <td>17.0</td>\n",
  5495. " </tr>\n",
  5496. " <tr>\n",
  5497. " <th>zimmery01</th>\n",
  5498. " <th>2016</th>\n",
  5499. " <td>115</td>\n",
  5500. " <td>427</td>\n",
  5501. " <td>60</td>\n",
  5502. " <td>93</td>\n",
  5503. " <td>18</td>\n",
  5504. " <td>1</td>\n",
  5505. " <td>15</td>\n",
  5506. " <td>46.0</td>\n",
  5507. " <td>4.0</td>\n",
  5508. " <td>1.0</td>\n",
  5509. " <td>29</td>\n",
  5510. " <td>104.0</td>\n",
  5511. " <td>1.0</td>\n",
  5512. " <td>5.0</td>\n",
  5513. " <td>0.0</td>\n",
  5514. " <td>6.0</td>\n",
  5515. " <td>12.0</td>\n",
  5516. " </tr>\n",
  5517. " </tbody>\n",
  5518. "</table>\n",
  5519. "</div>"
  5520. ],
  5521. "text/plain": [
  5522. " G AB R H 2B 3B HR RBI SB CS BB SO \\\n",
  5523. "desmoia01 2015 156 583 69 136 27 2 19 62.0 13.0 5.0 45 187.0 \n",
  5524. "escobyu01 2015 139 535 75 168 25 1 9 56.0 2.0 2.0 45 70.0 \n",
  5525. "espinda01 2015 118 367 59 88 21 1 13 37.0 5.0 2.0 33 106.0 \n",
  5526. " 2016 157 516 66 108 15 0 24 72.0 9.0 2.0 54 174.0 \n",
  5527. "harpebr03 2015 153 521 118 172 38 1 42 99.0 6.0 4.0 124 131.0 \n",
  5528. " 2016 147 506 84 123 24 2 24 86.0 21.0 10.0 108 117.0 \n",
  5529. "murphda08 2016 142 531 88 184 47 5 25 104.0 5.0 3.0 35 57.0 \n",
  5530. "ramoswi01 2015 128 475 41 109 16 0 15 68.0 0.0 0.0 21 101.0 \n",
  5531. " 2016 131 482 58 148 25 0 22 80.0 0.0 0.0 35 79.0 \n",
  5532. "rendoan01 2016 156 567 91 153 38 2 20 85.0 12.0 6.0 65 117.0 \n",
  5533. "reverbe01 2016 103 350 44 76 9 7 2 24.0 14.0 5.0 18 34.0 \n",
  5534. "robincl01 2015 126 309 44 84 15 1 10 34.0 0.0 0.0 37 52.0 \n",
  5535. " 2016 104 196 16 46 4 0 5 26.0 0.0 0.0 20 38.0 \n",
  5536. "taylomi02 2015 138 472 49 108 15 2 14 63.0 16.0 3.0 35 158.0 \n",
  5537. "werthja01 2016 143 525 84 128 28 0 21 69.0 5.0 1.0 71 139.0 \n",
  5538. "zimmery01 2016 115 427 60 93 18 1 15 46.0 4.0 1.0 29 104.0 \n",
  5539. "\n",
  5540. " IBB HBP SH SF GIDP \n",
  5541. "desmoia01 2015 0.0 3.0 6.0 4.0 9.0 \n",
  5542. "escobyu01 2015 0.0 8.0 1.0 2.0 24.0 \n",
  5543. "espinda01 2015 5.0 6.0 3.0 3.0 6.0 \n",
  5544. " 2016 12.0 20.0 7.0 4.0 4.0 \n",
  5545. "harpebr03 2015 15.0 5.0 0.0 4.0 15.0 \n",
  5546. " 2016 20.0 3.0 0.0 10.0 11.0 \n",
  5547. "murphda08 2016 10.0 8.0 0.0 8.0 4.0 \n",
  5548. "ramoswi01 2015 2.0 0.0 0.0 8.0 16.0 \n",
  5549. " 2016 2.0 2.0 0.0 4.0 17.0 \n",
  5550. "rendoan01 2016 2.0 7.0 0.0 8.0 5.0 \n",
  5551. "reverbe01 2016 0.0 3.0 2.0 2.0 12.0 \n",
  5552. "robincl01 2015 4.0 5.0 0.0 1.0 6.0 \n",
  5553. " 2016 0.0 2.0 1.0 5.0 4.0 \n",
  5554. "taylomi02 2015 9.0 1.0 1.0 2.0 5.0 \n",
  5555. "werthja01 2016 0.0 4.0 0.0 6.0 17.0 \n",
  5556. "zimmery01 2016 1.0 5.0 0.0 6.0 12.0 "
  5557. ]
  5558. },
  5559. "execution_count": 33,
  5560. "metadata": {},
  5561. "output_type": "execute_result"
  5562. }
  5563. ],
  5564. "source": [
  5565. "# sorting the indices is critical for lining up the data in the tuples\n",
  5566. "df_was = df_was.sort_values(by=['playerID']).\\\n",
  5567. " set_index(idx).\\\n",
  5568. " drop(['playerID', 'yearID', 'teamID', 'lgID', 'stint'], axis=1)\n",
  5569. "df_was"
  5570. ]
  5571. },
  5572. {
  5573. "cell_type": "code",
  5574. "execution_count": 34,
  5575. "metadata": {},
  5576. "outputs": [
  5577. {
  5578. "data": {
  5579. "text/html": [
  5580. "<div>\n",
  5581. "<style>\n",
  5582. " .dataframe thead tr:only-child th {\n",
  5583. " text-align: right;\n",
  5584. " }\n",
  5585. "\n",
  5586. " .dataframe thead th {\n",
  5587. " text-align: left;\n",
  5588. " }\n",
  5589. "\n",
  5590. " .dataframe tbody tr th {\n",
  5591. " vertical-align: top;\n",
  5592. " }\n",
  5593. "</style>\n",
  5594. "<table border=\"1\" class=\"dataframe\">\n",
  5595. " <thead>\n",
  5596. " <tr style=\"text-align: right;\">\n",
  5597. " <th></th>\n",
  5598. " <th>G</th>\n",
  5599. " <th>AB</th>\n",
  5600. " <th>H</th>\n",
  5601. " <th>SO</th>\n",
  5602. " </tr>\n",
  5603. " </thead>\n",
  5604. " <tbody>\n",
  5605. " <tr>\n",
  5606. " <th>2015</th>\n",
  5607. " <td>126</td>\n",
  5608. " <td>309</td>\n",
  5609. " <td>84</td>\n",
  5610. " <td>52.0</td>\n",
  5611. " </tr>\n",
  5612. " <tr>\n",
  5613. " <th>2016</th>\n",
  5614. " <td>104</td>\n",
  5615. " <td>196</td>\n",
  5616. " <td>46</td>\n",
  5617. " <td>38.0</td>\n",
  5618. " </tr>\n",
  5619. " </tbody>\n",
  5620. "</table>\n",
  5621. "</div>"
  5622. ],
  5623. "text/plain": [
  5624. " G AB H SO\n",
  5625. "2015 126 309 84 52.0\n",
  5626. "2016 104 196 46 38.0"
  5627. ]
  5628. },
  5629. "execution_count": 34,
  5630. "metadata": {},
  5631. "output_type": "execute_result"
  5632. }
  5633. ],
  5634. "source": [
  5635. "df_was.loc[('robincl01', ),['G', 'AB', 'H', 'SO']]"
  5636. ]
  5637. },
  5638. {
  5639. "cell_type": "code",
  5640. "execution_count": 35,
  5641. "metadata": {},
  5642. "outputs": [
  5643. {
  5644. "data": {
  5645. "text/plain": [
  5646. "G 104.0\n",
  5647. "AB 196.0\n",
  5648. "H 46.0\n",
  5649. "SO 38.0\n",
  5650. "Name: (robincl01, 2016), dtype: float64"
  5651. ]
  5652. },
  5653. "execution_count": 35,
  5654. "metadata": {},
  5655. "output_type": "execute_result"
  5656. }
  5657. ],
  5658. "source": [
  5659. "df_was.loc[('robincl01', 2016),['G', 'AB', 'H', 'SO']]"
  5660. ]
  5661. },
  5662. {
  5663. "cell_type": "markdown",
  5664. "metadata": {},
  5665. "source": [
  5666. "For the sake of the example, let's take the DataFrame for all rows of data past 2016 and create a multi-index using year, league, team and player as the groupings of the index."
  5667. ]
  5668. },
  5669. {
  5670. "cell_type": "code",
  5671. "execution_count": 36,
  5672. "metadata": {},
  5673. "outputs": [
  5674. {
  5675. "data": {
  5676. "text/html": [
  5677. "<div>\n",
  5678. "<style>\n",
  5679. " .dataframe thead tr:only-child th {\n",
  5680. " text-align: right;\n",
  5681. " }\n",
  5682. "\n",
  5683. " .dataframe thead th {\n",
  5684. " text-align: left;\n",
  5685. " }\n",
  5686. "\n",
  5687. " .dataframe tbody tr th {\n",
  5688. " vertical-align: top;\n",
  5689. " }\n",
  5690. "</style>\n",
  5691. "<table border=\"1\" class=\"dataframe\">\n",
  5692. " <thead>\n",
  5693. " <tr style=\"text-align: right;\">\n",
  5694. " <th></th>\n",
  5695. " <th>playerID</th>\n",
  5696. " <th>yearID</th>\n",
  5697. " <th>stint</th>\n",
  5698. " <th>teamID</th>\n",
  5699. " <th>lgID</th>\n",
  5700. " <th>G</th>\n",
  5701. " <th>AB</th>\n",
  5702. " <th>R</th>\n",
  5703. " <th>H</th>\n",
  5704. " <th>2B</th>\n",
  5705. " <th>...</th>\n",
  5706. " <th>RBI</th>\n",
  5707. " <th>SB</th>\n",
  5708. " <th>CS</th>\n",
  5709. " <th>BB</th>\n",
  5710. " <th>SO</th>\n",
  5711. " <th>IBB</th>\n",
  5712. " <th>HBP</th>\n",
  5713. " <th>SH</th>\n",
  5714. " <th>SF</th>\n",
  5715. " <th>GIDP</th>\n",
  5716. " </tr>\n",
  5717. " </thead>\n",
  5718. " <tbody>\n",
  5719. " <tr>\n",
  5720. " <th>0</th>\n",
  5721. " <td>abercda01</td>\n",
  5722. " <td>1871</td>\n",
  5723. " <td>1</td>\n",
  5724. " <td>TRO</td>\n",
  5725. " <td>NaN</td>\n",
  5726. " <td>1</td>\n",
  5727. " <td>4</td>\n",
  5728. " <td>0</td>\n",
  5729. " <td>0</td>\n",
  5730. " <td>0</td>\n",
  5731. " <td>...</td>\n",
  5732. " <td>0.0</td>\n",
  5733. " <td>0.0</td>\n",
  5734. " <td>0.0</td>\n",
  5735. " <td>0</td>\n",
  5736. " <td>0.0</td>\n",
  5737. " <td>NaN</td>\n",
  5738. " <td>NaN</td>\n",
  5739. " <td>NaN</td>\n",
  5740. " <td>NaN</td>\n",
  5741. " <td>NaN</td>\n",
  5742. " </tr>\n",
  5743. " <tr>\n",
  5744. " <th>1</th>\n",
  5745. " <td>addybo01</td>\n",
  5746. " <td>1871</td>\n",
  5747. " <td>1</td>\n",
  5748. " <td>RC1</td>\n",
  5749. " <td>NaN</td>\n",
  5750. " <td>25</td>\n",
  5751. " <td>118</td>\n",
  5752. " <td>30</td>\n",
  5753. " <td>32</td>\n",
  5754. " <td>6</td>\n",
  5755. " <td>...</td>\n",
  5756. " <td>13.0</td>\n",
  5757. " <td>8.0</td>\n",
  5758. " <td>1.0</td>\n",
  5759. " <td>4</td>\n",
  5760. " <td>0.0</td>\n",
  5761. " <td>NaN</td>\n",
  5762. " <td>NaN</td>\n",
  5763. " <td>NaN</td>\n",
  5764. " <td>NaN</td>\n",
  5765. " <td>NaN</td>\n",
  5766. " </tr>\n",
  5767. " <tr>\n",
  5768. " <th>2</th>\n",
  5769. " <td>allisar01</td>\n",
  5770. " <td>1871</td>\n",
  5771. " <td>1</td>\n",
  5772. " <td>CL1</td>\n",
  5773. " <td>NaN</td>\n",
  5774. " <td>29</td>\n",
  5775. " <td>137</td>\n",
  5776. " <td>28</td>\n",
  5777. " <td>40</td>\n",
  5778. " <td>4</td>\n",
  5779. " <td>...</td>\n",
  5780. " <td>19.0</td>\n",
  5781. " <td>3.0</td>\n",
  5782. " <td>1.0</td>\n",
  5783. " <td>2</td>\n",
  5784. " <td>5.0</td>\n",
  5785. " <td>NaN</td>\n",
  5786. " <td>NaN</td>\n",
  5787. " <td>NaN</td>\n",
  5788. " <td>NaN</td>\n",
  5789. " <td>NaN</td>\n",
  5790. " </tr>\n",
  5791. " <tr>\n",
  5792. " <th>3</th>\n",
  5793. " <td>allisdo01</td>\n",
  5794. " <td>1871</td>\n",
  5795. " <td>1</td>\n",
  5796. " <td>WS3</td>\n",
  5797. " <td>NaN</td>\n",
  5798. " <td>27</td>\n",
  5799. " <td>133</td>\n",
  5800. " <td>28</td>\n",
  5801. " <td>44</td>\n",
  5802. " <td>10</td>\n",
  5803. " <td>...</td>\n",
  5804. " <td>27.0</td>\n",
  5805. " <td>1.0</td>\n",
  5806. " <td>1.0</td>\n",
  5807. " <td>0</td>\n",
  5808. " <td>2.0</td>\n",
  5809. " <td>NaN</td>\n",
  5810. " <td>NaN</td>\n",
  5811. " <td>NaN</td>\n",
  5812. " <td>NaN</td>\n",
  5813. " <td>NaN</td>\n",
  5814. " </tr>\n",
  5815. " <tr>\n",
  5816. " <th>4</th>\n",
  5817. " <td>ansonca01</td>\n",
  5818. " <td>1871</td>\n",
  5819. " <td>1</td>\n",
  5820. " <td>RC1</td>\n",
  5821. " <td>NaN</td>\n",
  5822. " <td>25</td>\n",
  5823. " <td>120</td>\n",
  5824. " <td>29</td>\n",
  5825. " <td>39</td>\n",
  5826. " <td>11</td>\n",
  5827. " <td>...</td>\n",
  5828. " <td>16.0</td>\n",
  5829. " <td>6.0</td>\n",
  5830. " <td>2.0</td>\n",
  5831. " <td>2</td>\n",
  5832. " <td>1.0</td>\n",
  5833. " <td>NaN</td>\n",
  5834. " <td>NaN</td>\n",
  5835. " <td>NaN</td>\n",
  5836. " <td>NaN</td>\n",
  5837. " <td>NaN</td>\n",
  5838. " </tr>\n",
  5839. " </tbody>\n",
  5840. "</table>\n",
  5841. "<p>5 rows × 22 columns</p>\n",
  5842. "</div>"
  5843. ],
  5844. "text/plain": [
  5845. " playerID yearID stint teamID lgID G AB R H 2B ... RBI SB \\\n",
  5846. "0 abercda01 1871 1 TRO NaN 1 4 0 0 0 ... 0.0 0.0 \n",
  5847. "1 addybo01 1871 1 RC1 NaN 25 118 30 32 6 ... 13.0 8.0 \n",
  5848. "2 allisar01 1871 1 CL1 NaN 29 137 28 40 4 ... 19.0 3.0 \n",
  5849. "3 allisdo01 1871 1 WS3 NaN 27 133 28 44 10 ... 27.0 1.0 \n",
  5850. "4 ansonca01 1871 1 RC1 NaN 25 120 29 39 11 ... 16.0 6.0 \n",
  5851. "\n",
  5852. " CS BB SO IBB HBP SH SF GIDP \n",
  5853. "0 0.0 0 0.0 NaN NaN NaN NaN NaN \n",
  5854. "1 1.0 4 0.0 NaN NaN NaN NaN NaN \n",
  5855. "2 1.0 2 5.0 NaN NaN NaN NaN NaN \n",
  5856. "3 1.0 0 2.0 NaN NaN NaN NaN NaN \n",
  5857. "4 2.0 2 1.0 NaN NaN NaN NaN NaN \n",
  5858. "\n",
  5859. "[5 rows x 22 columns]"
  5860. ]
  5861. },
  5862. "execution_count": 36,
  5863. "metadata": {},
  5864. "output_type": "execute_result"
  5865. }
  5866. ],
  5867. "source": [
  5868. "df.head()"
  5869. ]
  5870. },
  5871. {
  5872. "cell_type": "code",
  5873. "execution_count": 37,
  5874. "metadata": {},
  5875. "outputs": [
  5876. {
  5877. "data": {
  5878. "text/plain": [
  5879. "((2016, 'NL', 'WAS', 'rzepcma01'),\n",
  5880. " (2016, 'NL', 'WAS', 'scherma01'),\n",
  5881. " (2016, 'NL', 'WAS', 'severpe01'),\n",
  5882. " (2016, 'NL', 'WAS', 'solissa01'),\n",
  5883. " (2016, 'NL', 'WAS', 'strasst01'),\n",
  5884. " (2016, 'NL', 'WAS', 'taylomi02'),\n",
  5885. " (2016, 'NL', 'WAS', 'treinbl01'),\n",
  5886. " (2016, 'NL', 'WAS', 'turnetr01'),\n",
  5887. " (2016, 'NL', 'WAS', 'werthja01'),\n",
  5888. " (2016, 'NL', 'WAS', 'zimmery01'))"
  5889. ]
  5890. },
  5891. "execution_count": 37,
  5892. "metadata": {},
  5893. "output_type": "execute_result"
  5894. }
  5895. ],
  5896. "source": [
  5897. "df_mi = df[df.yearID>2006].copy()\n",
  5898. "idx_labels = ['yearID', 'lgID', 'teamID', 'playerID']\n",
  5899. "\n",
  5900. "tuple(\n",
  5901. " zip(\n",
  5902. " df_mi[idx_labels]\\\n",
  5903. " .sort_values(idx_labels)['yearID'],\n",
  5904. "\n",
  5905. " df_mi[idx_labels]\\\n",
  5906. " .sort_values(idx_labels)['lgID'],\n",
  5907. "\n",
  5908. " df_mi[idx_labels]\\\n",
  5909. " .sort_values(idx_labels)['teamID'],\n",
  5910. "\n",
  5911. " df_mi[idx_labels]\\\n",
  5912. " .sort_values(idx_labels)['playerID']))[-10:]"
  5913. ]
  5914. },
  5915. {
  5916. "cell_type": "code",
  5917. "execution_count": 38,
  5918. "metadata": {
  5919. "collapsed": true
  5920. },
  5921. "outputs": [],
  5922. "source": [
  5923. "idx = \\\n",
  5924. " pd.MultiIndex.from_tuples(\n",
  5925. " tuple(\n",
  5926. " zip(\n",
  5927. " df_mi[idx_labels]\\\n",
  5928. " .sort_values(idx_labels)['yearID'],\n",
  5929. " \n",
  5930. " df_mi[idx_labels]\\\n",
  5931. " .sort_values(idx_labels)['lgID'],\n",
  5932. " \n",
  5933. " df_mi[idx_labels]\\\n",
  5934. " .sort_values(idx_labels)['teamID'],\n",
  5935. " \n",
  5936. " df_mi[idx_labels]\\\n",
  5937. " .sort_values(idx_labels)['playerID']))\n",
  5938. " )"
  5939. ]
  5940. },
  5941. {
  5942. "cell_type": "code",
  5943. "execution_count": 39,
  5944. "metadata": {
  5945. "collapsed": true
  5946. },
  5947. "outputs": [],
  5948. "source": [
  5949. "df_mi = df_mi.sort_values(['yearID', 'teamID']).set_index(idx)#.drop(['playerID', 'yearID', 'teamID', 'stint'], axis=1)"
  5950. ]
  5951. },
  5952. {
  5953. "cell_type": "code",
  5954. "execution_count": 40,
  5955. "metadata": {},
  5956. "outputs": [
  5957. {
  5958. "data": {
  5959. "text/html": [
  5960. "<div>\n",
  5961. "<style>\n",
  5962. " .dataframe thead tr:only-child th {\n",
  5963. " text-align: right;\n",
  5964. " }\n",
  5965. "\n",
  5966. " .dataframe thead th {\n",
  5967. " text-align: left;\n",
  5968. " }\n",
  5969. "\n",
  5970. " .dataframe tbody tr th {\n",
  5971. " vertical-align: top;\n",
  5972. " }\n",
  5973. "</style>\n",
  5974. "<table border=\"1\" class=\"dataframe\">\n",
  5975. " <thead>\n",
  5976. " <tr style=\"text-align: right;\">\n",
  5977. " <th></th>\n",
  5978. " <th></th>\n",
  5979. " <th></th>\n",
  5980. " <th></th>\n",
  5981. " <th>playerID</th>\n",
  5982. " <th>yearID</th>\n",
  5983. " <th>stint</th>\n",
  5984. " <th>teamID</th>\n",
  5985. " <th>lgID</th>\n",
  5986. " <th>G</th>\n",
  5987. " <th>AB</th>\n",
  5988. " <th>R</th>\n",
  5989. " <th>H</th>\n",
  5990. " <th>2B</th>\n",
  5991. " <th>...</th>\n",
  5992. " <th>RBI</th>\n",
  5993. " <th>SB</th>\n",
  5994. " <th>CS</th>\n",
  5995. " <th>BB</th>\n",
  5996. " <th>SO</th>\n",
  5997. " <th>IBB</th>\n",
  5998. " <th>HBP</th>\n",
  5999. " <th>SH</th>\n",
  6000. " <th>SF</th>\n",
  6001. " <th>GIDP</th>\n",
  6002. " </tr>\n",
  6003. " </thead>\n",
  6004. " <tbody>\n",
  6005. " <tr>\n",
  6006. " <th rowspan=\"5\" valign=\"top\">2007</th>\n",
  6007. " <th rowspan=\"5\" valign=\"top\">AL</th>\n",
  6008. " <th rowspan=\"5\" valign=\"top\">BAL</th>\n",
  6009. " <th>baezda01</th>\n",
  6010. " <td>bardebr01</td>\n",
  6011. " <td>2007</td>\n",
  6012. " <td>1</td>\n",
  6013. " <td>ARI</td>\n",
  6014. " <td>NL</td>\n",
  6015. " <td>8</td>\n",
  6016. " <td>12</td>\n",
  6017. " <td>0</td>\n",
  6018. " <td>1</td>\n",
  6019. " <td>0</td>\n",
  6020. " <td>...</td>\n",
  6021. " <td>0.0</td>\n",
  6022. " <td>0.0</td>\n",
  6023. " <td>0.0</td>\n",
  6024. " <td>0</td>\n",
  6025. " <td>3.0</td>\n",
  6026. " <td>0.0</td>\n",
  6027. " <td>0.0</td>\n",
  6028. " <td>0.0</td>\n",
  6029. " <td>0.0</td>\n",
  6030. " <td>0.0</td>\n",
  6031. " </tr>\n",
  6032. " <tr>\n",
  6033. " <th>bakopa01</th>\n",
  6034. " <td>bonifem01</td>\n",
  6035. " <td>2007</td>\n",
  6036. " <td>1</td>\n",
  6037. " <td>ARI</td>\n",
  6038. " <td>NL</td>\n",
  6039. " <td>11</td>\n",
  6040. " <td>23</td>\n",
  6041. " <td>2</td>\n",
  6042. " <td>5</td>\n",
  6043. " <td>1</td>\n",
  6044. " <td>...</td>\n",
  6045. " <td>2.0</td>\n",
  6046. " <td>0.0</td>\n",
  6047. " <td>1.0</td>\n",
  6048. " <td>4</td>\n",
  6049. " <td>3.0</td>\n",
  6050. " <td>0.0</td>\n",
  6051. " <td>0.0</td>\n",
  6052. " <td>0.0</td>\n",
  6053. " <td>0.0</td>\n",
  6054. " <td>0.0</td>\n",
  6055. " </tr>\n",
  6056. " <tr>\n",
  6057. " <th>bedarer01</th>\n",
  6058. " <td>byrneer01</td>\n",
  6059. " <td>2007</td>\n",
  6060. " <td>1</td>\n",
  6061. " <td>ARI</td>\n",
  6062. " <td>NL</td>\n",
  6063. " <td>160</td>\n",
  6064. " <td>626</td>\n",
  6065. " <td>103</td>\n",
  6066. " <td>179</td>\n",
  6067. " <td>30</td>\n",
  6068. " <td>...</td>\n",
  6069. " <td>83.0</td>\n",
  6070. " <td>50.0</td>\n",
  6071. " <td>7.0</td>\n",
  6072. " <td>57</td>\n",
  6073. " <td>98.0</td>\n",
  6074. " <td>5.0</td>\n",
  6075. " <td>10.0</td>\n",
  6076. " <td>1.0</td>\n",
  6077. " <td>4.0</td>\n",
  6078. " <td>12.0</td>\n",
  6079. " </tr>\n",
  6080. " <tr>\n",
  6081. " <th>bellro01</th>\n",
  6082. " <td>callaal01</td>\n",
  6083. " <td>2007</td>\n",
  6084. " <td>1</td>\n",
  6085. " <td>ARI</td>\n",
  6086. " <td>NL</td>\n",
  6087. " <td>56</td>\n",
  6088. " <td>144</td>\n",
  6089. " <td>10</td>\n",
  6090. " <td>31</td>\n",
  6091. " <td>8</td>\n",
  6092. " <td>...</td>\n",
  6093. " <td>7.0</td>\n",
  6094. " <td>1.0</td>\n",
  6095. " <td>1.0</td>\n",
  6096. " <td>9</td>\n",
  6097. " <td>14.0</td>\n",
  6098. " <td>0.0</td>\n",
  6099. " <td>1.0</td>\n",
  6100. " <td>1.0</td>\n",
  6101. " <td>1.0</td>\n",
  6102. " <td>8.0</td>\n",
  6103. " </tr>\n",
  6104. " <tr>\n",
  6105. " <th>birkiku01</th>\n",
  6106. " <td>choatra01</td>\n",
  6107. " <td>2007</td>\n",
  6108. " <td>1</td>\n",
  6109. " <td>ARI</td>\n",
  6110. " <td>NL</td>\n",
  6111. " <td>2</td>\n",
  6112. " <td>0</td>\n",
  6113. " <td>0</td>\n",
  6114. " <td>0</td>\n",
  6115. " <td>0</td>\n",
  6116. " <td>...</td>\n",
  6117. " <td>0.0</td>\n",
  6118. " <td>0.0</td>\n",
  6119. " <td>0.0</td>\n",
  6120. " <td>0</td>\n",
  6121. " <td>0.0</td>\n",
  6122. " <td>0.0</td>\n",
  6123. " <td>0.0</td>\n",
  6124. " <td>0.0</td>\n",
  6125. " <td>0.0</td>\n",
  6126. " <td>0.0</td>\n",
  6127. " </tr>\n",
  6128. " </tbody>\n",
  6129. "</table>\n",
  6130. "<p>5 rows × 22 columns</p>\n",
  6131. "</div>"
  6132. ],
  6133. "text/plain": [
  6134. " playerID yearID stint teamID lgID G AB R \\\n",
  6135. "2007 AL BAL baezda01 bardebr01 2007 1 ARI NL 8 12 0 \n",
  6136. " bakopa01 bonifem01 2007 1 ARI NL 11 23 2 \n",
  6137. " bedarer01 byrneer01 2007 1 ARI NL 160 626 103 \n",
  6138. " bellro01 callaal01 2007 1 ARI NL 56 144 10 \n",
  6139. " birkiku01 choatra01 2007 1 ARI NL 2 0 0 \n",
  6140. "\n",
  6141. " H 2B ... RBI SB CS BB SO IBB HBP \\\n",
  6142. "2007 AL BAL baezda01 1 0 ... 0.0 0.0 0.0 0 3.0 0.0 0.0 \n",
  6143. " bakopa01 5 1 ... 2.0 0.0 1.0 4 3.0 0.0 0.0 \n",
  6144. " bedarer01 179 30 ... 83.0 50.0 7.0 57 98.0 5.0 10.0 \n",
  6145. " bellro01 31 8 ... 7.0 1.0 1.0 9 14.0 0.0 1.0 \n",
  6146. " birkiku01 0 0 ... 0.0 0.0 0.0 0 0.0 0.0 0.0 \n",
  6147. "\n",
  6148. " SH SF GIDP \n",
  6149. "2007 AL BAL baezda01 0.0 0.0 0.0 \n",
  6150. " bakopa01 0.0 0.0 0.0 \n",
  6151. " bedarer01 1.0 4.0 12.0 \n",
  6152. " bellro01 1.0 1.0 8.0 \n",
  6153. " birkiku01 0.0 0.0 0.0 \n",
  6154. "\n",
  6155. "[5 rows x 22 columns]"
  6156. ]
  6157. },
  6158. "execution_count": 40,
  6159. "metadata": {},
  6160. "output_type": "execute_result"
  6161. }
  6162. ],
  6163. "source": [
  6164. "df_mi.head()"
  6165. ]
  6166. },
  6167. {
  6168. "cell_type": "code",
  6169. "execution_count": 41,
  6170. "metadata": {},
  6171. "outputs": [
  6172. {
  6173. "data": {
  6174. "text/html": [
  6175. "<div>\n",
  6176. "<style>\n",
  6177. " .dataframe thead tr:only-child th {\n",
  6178. " text-align: right;\n",
  6179. " }\n",
  6180. "\n",
  6181. " .dataframe thead th {\n",
  6182. " text-align: left;\n",
  6183. " }\n",
  6184. "\n",
  6185. " .dataframe tbody tr th {\n",
  6186. " vertical-align: top;\n",
  6187. " }\n",
  6188. "</style>\n",
  6189. "<table border=\"1\" class=\"dataframe\">\n",
  6190. " <thead>\n",
  6191. " <tr style=\"text-align: right;\">\n",
  6192. " <th></th>\n",
  6193. " <th></th>\n",
  6194. " <th></th>\n",
  6195. " <th></th>\n",
  6196. " <th>playerID</th>\n",
  6197. " <th>yearID</th>\n",
  6198. " <th>stint</th>\n",
  6199. " <th>teamID</th>\n",
  6200. " <th>lgID</th>\n",
  6201. " <th>G</th>\n",
  6202. " <th>AB</th>\n",
  6203. " <th>R</th>\n",
  6204. " <th>H</th>\n",
  6205. " <th>2B</th>\n",
  6206. " <th>...</th>\n",
  6207. " <th>RBI</th>\n",
  6208. " <th>SB</th>\n",
  6209. " <th>CS</th>\n",
  6210. " <th>BB</th>\n",
  6211. " <th>SO</th>\n",
  6212. " <th>IBB</th>\n",
  6213. " <th>HBP</th>\n",
  6214. " <th>SH</th>\n",
  6215. " <th>SF</th>\n",
  6216. " <th>GIDP</th>\n",
  6217. " </tr>\n",
  6218. " </thead>\n",
  6219. " <tbody>\n",
  6220. " <tr>\n",
  6221. " <th rowspan=\"5\" valign=\"top\">2016</th>\n",
  6222. " <th rowspan=\"5\" valign=\"top\">NL</th>\n",
  6223. " <th rowspan=\"5\" valign=\"top\">WAS</th>\n",
  6224. " <th>taylomi02</th>\n",
  6225. " <td>taylomi02</td>\n",
  6226. " <td>2016</td>\n",
  6227. " <td>1</td>\n",
  6228. " <td>WAS</td>\n",
  6229. " <td>NL</td>\n",
  6230. " <td>76</td>\n",
  6231. " <td>221</td>\n",
  6232. " <td>28</td>\n",
  6233. " <td>51</td>\n",
  6234. " <td>11</td>\n",
  6235. " <td>...</td>\n",
  6236. " <td>16.0</td>\n",
  6237. " <td>14.0</td>\n",
  6238. " <td>3.0</td>\n",
  6239. " <td>14</td>\n",
  6240. " <td>77.0</td>\n",
  6241. " <td>0.0</td>\n",
  6242. " <td>1.0</td>\n",
  6243. " <td>0.0</td>\n",
  6244. " <td>1.0</td>\n",
  6245. " <td>2.0</td>\n",
  6246. " </tr>\n",
  6247. " <tr>\n",
  6248. " <th>treinbl01</th>\n",
  6249. " <td>treinbl01</td>\n",
  6250. " <td>2016</td>\n",
  6251. " <td>1</td>\n",
  6252. " <td>WAS</td>\n",
  6253. " <td>NL</td>\n",
  6254. " <td>73</td>\n",
  6255. " <td>0</td>\n",
  6256. " <td>0</td>\n",
  6257. " <td>0</td>\n",
  6258. " <td>0</td>\n",
  6259. " <td>...</td>\n",
  6260. " <td>0.0</td>\n",
  6261. " <td>0.0</td>\n",
  6262. " <td>0.0</td>\n",
  6263. " <td>0</td>\n",
  6264. " <td>0.0</td>\n",
  6265. " <td>0.0</td>\n",
  6266. " <td>0.0</td>\n",
  6267. " <td>0.0</td>\n",
  6268. " <td>0.0</td>\n",
  6269. " <td>0.0</td>\n",
  6270. " </tr>\n",
  6271. " <tr>\n",
  6272. " <th>turnetr01</th>\n",
  6273. " <td>turnetr01</td>\n",
  6274. " <td>2016</td>\n",
  6275. " <td>1</td>\n",
  6276. " <td>WAS</td>\n",
  6277. " <td>NL</td>\n",
  6278. " <td>73</td>\n",
  6279. " <td>307</td>\n",
  6280. " <td>53</td>\n",
  6281. " <td>105</td>\n",
  6282. " <td>14</td>\n",
  6283. " <td>...</td>\n",
  6284. " <td>40.0</td>\n",
  6285. " <td>33.0</td>\n",
  6286. " <td>6.0</td>\n",
  6287. " <td>14</td>\n",
  6288. " <td>59.0</td>\n",
  6289. " <td>0.0</td>\n",
  6290. " <td>1.0</td>\n",
  6291. " <td>0.0</td>\n",
  6292. " <td>2.0</td>\n",
  6293. " <td>1.0</td>\n",
  6294. " </tr>\n",
  6295. " <tr>\n",
  6296. " <th>werthja01</th>\n",
  6297. " <td>werthja01</td>\n",
  6298. " <td>2016</td>\n",
  6299. " <td>1</td>\n",
  6300. " <td>WAS</td>\n",
  6301. " <td>NL</td>\n",
  6302. " <td>143</td>\n",
  6303. " <td>525</td>\n",
  6304. " <td>84</td>\n",
  6305. " <td>128</td>\n",
  6306. " <td>28</td>\n",
  6307. " <td>...</td>\n",
  6308. " <td>69.0</td>\n",
  6309. " <td>5.0</td>\n",
  6310. " <td>1.0</td>\n",
  6311. " <td>71</td>\n",
  6312. " <td>139.0</td>\n",
  6313. " <td>0.0</td>\n",
  6314. " <td>4.0</td>\n",
  6315. " <td>0.0</td>\n",
  6316. " <td>6.0</td>\n",
  6317. " <td>17.0</td>\n",
  6318. " </tr>\n",
  6319. " <tr>\n",
  6320. " <th>zimmery01</th>\n",
  6321. " <td>zimmery01</td>\n",
  6322. " <td>2016</td>\n",
  6323. " <td>1</td>\n",
  6324. " <td>WAS</td>\n",
  6325. " <td>NL</td>\n",
  6326. " <td>115</td>\n",
  6327. " <td>427</td>\n",
  6328. " <td>60</td>\n",
  6329. " <td>93</td>\n",
  6330. " <td>18</td>\n",
  6331. " <td>...</td>\n",
  6332. " <td>46.0</td>\n",
  6333. " <td>4.0</td>\n",
  6334. " <td>1.0</td>\n",
  6335. " <td>29</td>\n",
  6336. " <td>104.0</td>\n",
  6337. " <td>1.0</td>\n",
  6338. " <td>5.0</td>\n",
  6339. " <td>0.0</td>\n",
  6340. " <td>6.0</td>\n",
  6341. " <td>12.0</td>\n",
  6342. " </tr>\n",
  6343. " </tbody>\n",
  6344. "</table>\n",
  6345. "<p>5 rows × 22 columns</p>\n",
  6346. "</div>"
  6347. ],
  6348. "text/plain": [
  6349. " playerID yearID stint teamID lgID G AB R \\\n",
  6350. "2016 NL WAS taylomi02 taylomi02 2016 1 WAS NL 76 221 28 \n",
  6351. " treinbl01 treinbl01 2016 1 WAS NL 73 0 0 \n",
  6352. " turnetr01 turnetr01 2016 1 WAS NL 73 307 53 \n",
  6353. " werthja01 werthja01 2016 1 WAS NL 143 525 84 \n",
  6354. " zimmery01 zimmery01 2016 1 WAS NL 115 427 60 \n",
  6355. "\n",
  6356. " H 2B ... RBI SB CS BB SO IBB HBP \\\n",
  6357. "2016 NL WAS taylomi02 51 11 ... 16.0 14.0 3.0 14 77.0 0.0 1.0 \n",
  6358. " treinbl01 0 0 ... 0.0 0.0 0.0 0 0.0 0.0 0.0 \n",
  6359. " turnetr01 105 14 ... 40.0 33.0 6.0 14 59.0 0.0 1.0 \n",
  6360. " werthja01 128 28 ... 69.0 5.0 1.0 71 139.0 0.0 4.0 \n",
  6361. " zimmery01 93 18 ... 46.0 4.0 1.0 29 104.0 1.0 5.0 \n",
  6362. "\n",
  6363. " SH SF GIDP \n",
  6364. "2016 NL WAS taylomi02 0.0 1.0 2.0 \n",
  6365. " treinbl01 0.0 0.0 0.0 \n",
  6366. " turnetr01 0.0 2.0 1.0 \n",
  6367. " werthja01 0.0 6.0 17.0 \n",
  6368. " zimmery01 0.0 6.0 12.0 \n",
  6369. "\n",
  6370. "[5 rows x 22 columns]"
  6371. ]
  6372. },
  6373. "execution_count": 41,
  6374. "metadata": {},
  6375. "output_type": "execute_result"
  6376. }
  6377. ],
  6378. "source": [
  6379. "df_mi.tail()"
  6380. ]
  6381. },
  6382. {
  6383. "cell_type": "markdown",
  6384. "metadata": {},
  6385. "source": [
  6386. "Now we can use this multi-index to out advantage, using the tuple of the index values we want and restricting the columns to just the data of interest."
  6387. ]
  6388. },
  6389. {
  6390. "cell_type": "code",
  6391. "execution_count": 42,
  6392. "metadata": {},
  6393. "outputs": [
  6394. {
  6395. "data": {
  6396. "text/html": [
  6397. "<div>\n",
  6398. "<style>\n",
  6399. " .dataframe thead tr:only-child th {\n",
  6400. " text-align: right;\n",
  6401. " }\n",
  6402. "\n",
  6403. " .dataframe thead th {\n",
  6404. " text-align: left;\n",
  6405. " }\n",
  6406. "\n",
  6407. " .dataframe tbody tr th {\n",
  6408. " vertical-align: top;\n",
  6409. " }\n",
  6410. "</style>\n",
  6411. "<table border=\"1\" class=\"dataframe\">\n",
  6412. " <thead>\n",
  6413. " <tr style=\"text-align: right;\">\n",
  6414. " <th></th>\n",
  6415. " <th>G</th>\n",
  6416. " <th>AB</th>\n",
  6417. " </tr>\n",
  6418. " </thead>\n",
  6419. " <tbody>\n",
  6420. " <tr>\n",
  6421. " <th>accarje01</th>\n",
  6422. " <td>152</td>\n",
  6423. " <td>509</td>\n",
  6424. " </tr>\n",
  6425. " <tr>\n",
  6426. " <th>adamsru01</th>\n",
  6427. " <td>62</td>\n",
  6428. " <td>1</td>\n",
  6429. " </tr>\n",
  6430. " <tr>\n",
  6431. " <th>banksjo01</th>\n",
  6432. " <td>26</td>\n",
  6433. " <td>5</td>\n",
  6434. " </tr>\n",
  6435. " <tr>\n",
  6436. " <th>burneaj01</th>\n",
  6437. " <td>8</td>\n",
  6438. " <td>14</td>\n",
  6439. " </tr>\n",
  6440. " <tr>\n",
  6441. " <th>chacigu01</th>\n",
  6442. " <td>65</td>\n",
  6443. " <td>0</td>\n",
  6444. " </tr>\n",
  6445. " </tbody>\n",
  6446. "</table>\n",
  6447. "</div>"
  6448. ],
  6449. "text/plain": [
  6450. " G AB\n",
  6451. "accarje01 152 509\n",
  6452. "adamsru01 62 1\n",
  6453. "banksjo01 26 5\n",
  6454. "burneaj01 8 14\n",
  6455. "chacigu01 65 0"
  6456. ]
  6457. },
  6458. "execution_count": 42,
  6459. "metadata": {},
  6460. "output_type": "execute_result"
  6461. }
  6462. ],
  6463. "source": [
  6464. "df_mi.loc[(2007, 'AL', 'TOR'), ['G', 'AB']].head()"
  6465. ]
  6466. },
  6467. {
  6468. "cell_type": "markdown",
  6469. "metadata": {},
  6470. "source": [
  6471. "&Xi;"
  6472. ]
  6473. }
  6474. ],
  6475. "metadata": {
  6476. "anaconda-cloud": {},
  6477. "gist": {
  6478. "data": {
  6479. "description": "nb/2_dataframe_operations.ipynb",
  6480. "public": false
  6481. },
  6482. "id": ""
  6483. },
  6484. "kernelspec": {
  6485. "display_name": "Python [default]",
  6486. "language": "python",
  6487. "name": "python3"
  6488. },
  6489. "language_info": {
  6490. "codemirror_mode": {
  6491. "name": "ipython",
  6492. "version": 3
  6493. },
  6494. "file_extension": ".py",
  6495. "mimetype": "text/x-python",
  6496. "name": "python",
  6497. "nbconvert_exporter": "python",
  6498. "pygments_lexer": "ipython3",
  6499. "version": "3.6.1"
  6500. },
  6501. "toc": {
  6502. "colors": {
  6503. "hover_highlight": "#DAA520",
  6504. "navigate_num": "#000000",
  6505. "navigate_text": "#333333",
  6506. "running_highlight": "#FF0000",
  6507. "selected_highlight": "#FFD700",
  6508. "sidebar_border": "#EEEEEE",
  6509. "wrapper_background": "#FFFFFF"
  6510. },
  6511. "moveMenuLeft": true,
  6512. "nav_menu": {
  6513. "height": "211px",
  6514. "width": "252px"
  6515. },
  6516. "navigate_menu": true,
  6517. "number_sections": false,
  6518. "sideBar": true,
  6519. "threshold": 4,
  6520. "toc_cell": true,
  6521. "toc_section_display": "block",
  6522. "toc_window_display": true,
  6523. "widenNotebook": false
  6524. }
  6525. },
  6526. "nbformat": 4,
  6527. "nbformat_minor": 2
  6528. }