|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "** NAVIGATION **\n",
- "\n",
- "**Got Pandas? _Practical Data Wrangling with Pandas_**\n",
- "\n",
- "* [Introduction](./0_introduction.ipynb)\n",
- "1. [Data Structures](./1_data_structures.ipynb)\n",
- "2. [Importing Data](./2_importing_data.ipynb)\n",
- "3. **Manipulating DataFrames**\n",
- "4. [Wrap Up](./4_wrapping_up.ipynb)\n",
- "---"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "toc": "true"
- },
- "source": [
- "# Table of Contents\n",
- " <p><div class=\"lev1 toc-item\"><a href=\"#Manipulating-DataFrames\" data-toc-modified-id=\"Manipulating-DataFrames-1\"><span class=\"toc-item-num\">1 </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 </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 </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 </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 </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 </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 </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 </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 </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 </span>Advanced indexing</a></div>"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "---"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Manipulating DataFrames"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "We will review our terminology for a quick moment:\n",
- "* **index** : the column and row indices of your Series or DataFrame, the index for each of these may be hiearchical\n",
- " * row index : the index along the horizontal dimension, and typically used as the primary index\n",
- " * column index : the index along the vertical dimension\n",
- " \n",
- " \n",
- "* **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",
- " \n",
- " \n",
- "**NOTEBOOK OBJECTIVES**\n",
- "\n",
- "In this notebook we'll:\n",
- "\n",
- "* explore more complex slicing and selecting, \n",
- "* look at DataFrame concatenation and appending,\n",
- "* explore Multi-Indices / hierarchical indexing in Pandas."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## More Selecting\n",
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import pandas as pd\n",
- "\n",
- "# get the data for players in 2015-16 who played in 100 or more games\n",
- "df = pd.read_csv(\"./datasets/Batting.csv\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### The convenient `[]` operator (_again_)\n",
- "\n",
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>yearID</th>\n",
- " <th>stint</th>\n",
- " <th>teamID</th>\n",
- " <th>lgID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>R</th>\n",
- " <th>H</th>\n",
- " <th>2B</th>\n",
- " <th>...</th>\n",
- " <th>RBI</th>\n",
- " <th>SB</th>\n",
- " <th>CS</th>\n",
- " <th>BB</th>\n",
- " <th>SO</th>\n",
- " <th>IBB</th>\n",
- " <th>HBP</th>\n",
- " <th>SH</th>\n",
- " <th>SF</th>\n",
- " <th>GIDP</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>0</th>\n",
- " <td>abercda01</td>\n",
- " <td>1871</td>\n",
- " <td>1</td>\n",
- " <td>TRO</td>\n",
- " <td>NaN</td>\n",
- " <td>1</td>\n",
- " <td>4</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>addybo01</td>\n",
- " <td>1871</td>\n",
- " <td>1</td>\n",
- " <td>RC1</td>\n",
- " <td>NaN</td>\n",
- " <td>25</td>\n",
- " <td>118</td>\n",
- " <td>30</td>\n",
- " <td>32</td>\n",
- " <td>6</td>\n",
- " <td>...</td>\n",
- " <td>13.0</td>\n",
- " <td>8.0</td>\n",
- " <td>1.0</td>\n",
- " <td>4</td>\n",
- " <td>0.0</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2</th>\n",
- " <td>allisar01</td>\n",
- " <td>1871</td>\n",
- " <td>1</td>\n",
- " <td>CL1</td>\n",
- " <td>NaN</td>\n",
- " <td>29</td>\n",
- " <td>137</td>\n",
- " <td>28</td>\n",
- " <td>40</td>\n",
- " <td>4</td>\n",
- " <td>...</td>\n",
- " <td>19.0</td>\n",
- " <td>3.0</td>\n",
- " <td>1.0</td>\n",
- " <td>2</td>\n",
- " <td>5.0</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>3</th>\n",
- " <td>allisdo01</td>\n",
- " <td>1871</td>\n",
- " <td>1</td>\n",
- " <td>WS3</td>\n",
- " <td>NaN</td>\n",
- " <td>27</td>\n",
- " <td>133</td>\n",
- " <td>28</td>\n",
- " <td>44</td>\n",
- " <td>10</td>\n",
- " <td>...</td>\n",
- " <td>27.0</td>\n",
- " <td>1.0</td>\n",
- " <td>1.0</td>\n",
- " <td>0</td>\n",
- " <td>2.0</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>4</th>\n",
- " <td>ansonca01</td>\n",
- " <td>1871</td>\n",
- " <td>1</td>\n",
- " <td>RC1</td>\n",
- " <td>NaN</td>\n",
- " <td>25</td>\n",
- " <td>120</td>\n",
- " <td>29</td>\n",
- " <td>39</td>\n",
- " <td>11</td>\n",
- " <td>...</td>\n",
- " <td>16.0</td>\n",
- " <td>6.0</td>\n",
- " <td>2.0</td>\n",
- " <td>2</td>\n",
- " <td>1.0</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "<p>5 rows × 22 columns</p>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID yearID stint teamID lgID G AB R H 2B ... RBI SB \\\n",
- "0 abercda01 1871 1 TRO NaN 1 4 0 0 0 ... 0.0 0.0 \n",
- "1 addybo01 1871 1 RC1 NaN 25 118 30 32 6 ... 13.0 8.0 \n",
- "2 allisar01 1871 1 CL1 NaN 29 137 28 40 4 ... 19.0 3.0 \n",
- "3 allisdo01 1871 1 WS3 NaN 27 133 28 44 10 ... 27.0 1.0 \n",
- "4 ansonca01 1871 1 RC1 NaN 25 120 29 39 11 ... 16.0 6.0 \n",
- "\n",
- " CS BB SO IBB HBP SH SF GIDP \n",
- "0 0.0 0 0.0 NaN NaN NaN NaN NaN \n",
- "1 1.0 4 0.0 NaN NaN NaN NaN NaN \n",
- "2 1.0 2 5.0 NaN NaN NaN NaN NaN \n",
- "3 1.0 0 2.0 NaN NaN NaN NaN NaN \n",
- "4 2.0 2 1.0 NaN NaN NaN NaN NaN \n",
- "\n",
- "[5 rows x 22 columns]"
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df[:5]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>yearID</th>\n",
- " <th>stint</th>\n",
- " <th>teamID</th>\n",
- " <th>lgID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>R</th>\n",
- " <th>H</th>\n",
- " <th>2B</th>\n",
- " <th>...</th>\n",
- " <th>RBI</th>\n",
- " <th>SB</th>\n",
- " <th>CS</th>\n",
- " <th>BB</th>\n",
- " <th>SO</th>\n",
- " <th>IBB</th>\n",
- " <th>HBP</th>\n",
- " <th>SH</th>\n",
- " <th>SF</th>\n",
- " <th>GIDP</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>102801</th>\n",
- " <td>ynoaga01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>NYN</td>\n",
- " <td>NL</td>\n",
- " <td>10</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102802</th>\n",
- " <td>ynoami01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>CHA</td>\n",
- " <td>AL</td>\n",
- " <td>23</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102803</th>\n",
- " <td>ynoara01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>COL</td>\n",
- " <td>NL</td>\n",
- " <td>3</td>\n",
- " <td>5</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>2.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102804</th>\n",
- " <td>youngch03</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>KCA</td>\n",
- " <td>AL</td>\n",
- " <td>34</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102805</th>\n",
- " <td>youngch04</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>BOS</td>\n",
- " <td>AL</td>\n",
- " <td>76</td>\n",
- " <td>203</td>\n",
- " <td>29</td>\n",
- " <td>56</td>\n",
- " <td>18</td>\n",
- " <td>...</td>\n",
- " <td>24.0</td>\n",
- " <td>4.0</td>\n",
- " <td>2.0</td>\n",
- " <td>21</td>\n",
- " <td>50.0</td>\n",
- " <td>0.0</td>\n",
- " <td>3.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>4.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102806</th>\n",
- " <td>younger03</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>NYA</td>\n",
- " <td>AL</td>\n",
- " <td>6</td>\n",
- " <td>1</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102807</th>\n",
- " <td>youngma03</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>ATL</td>\n",
- " <td>NL</td>\n",
- " <td>8</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102808</th>\n",
- " <td>zastrro01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>CHN</td>\n",
- " <td>NL</td>\n",
- " <td>8</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>2.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102809</th>\n",
- " <td>zieglbr01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>ARI</td>\n",
- " <td>NL</td>\n",
- " <td>36</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102810</th>\n",
- " <td>zieglbr01</td>\n",
- " <td>2016</td>\n",
- " <td>2</td>\n",
- " <td>BOS</td>\n",
- " <td>AL</td>\n",
- " <td>33</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102811</th>\n",
- " <td>zimmejo02</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>DET</td>\n",
- " <td>AL</td>\n",
- " <td>19</td>\n",
- " <td>4</td>\n",
- " <td>0</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>2.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102812</th>\n",
- " <td>zimmery01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>WAS</td>\n",
- " <td>NL</td>\n",
- " <td>115</td>\n",
- " <td>427</td>\n",
- " <td>60</td>\n",
- " <td>93</td>\n",
- " <td>18</td>\n",
- " <td>...</td>\n",
- " <td>46.0</td>\n",
- " <td>4.0</td>\n",
- " <td>1.0</td>\n",
- " <td>29</td>\n",
- " <td>104.0</td>\n",
- " <td>1.0</td>\n",
- " <td>5.0</td>\n",
- " <td>0.0</td>\n",
- " <td>6.0</td>\n",
- " <td>12.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102813</th>\n",
- " <td>zobribe01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>CHN</td>\n",
- " <td>NL</td>\n",
- " <td>147</td>\n",
- " <td>523</td>\n",
- " <td>94</td>\n",
- " <td>142</td>\n",
- " <td>31</td>\n",
- " <td>...</td>\n",
- " <td>76.0</td>\n",
- " <td>6.0</td>\n",
- " <td>4.0</td>\n",
- " <td>96</td>\n",
- " <td>82.0</td>\n",
- " <td>6.0</td>\n",
- " <td>4.0</td>\n",
- " <td>4.0</td>\n",
- " <td>4.0</td>\n",
- " <td>17.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102814</th>\n",
- " <td>zuninmi01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>SEA</td>\n",
- " <td>AL</td>\n",
- " <td>55</td>\n",
- " <td>164</td>\n",
- " <td>16</td>\n",
- " <td>34</td>\n",
- " <td>7</td>\n",
- " <td>...</td>\n",
- " <td>31.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>21</td>\n",
- " <td>65.0</td>\n",
- " <td>0.0</td>\n",
- " <td>6.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>102815</th>\n",
- " <td>zychto01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>SEA</td>\n",
- " <td>AL</td>\n",
- " <td>12</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "<p>15 rows × 22 columns</p>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID yearID stint teamID lgID G AB R H 2B ... \\\n",
- "102801 ynoaga01 2016 1 NYN NL 10 3 0 0 0 ... \n",
- "102802 ynoami01 2016 1 CHA AL 23 0 0 0 0 ... \n",
- "102803 ynoara01 2016 1 COL NL 3 5 0 0 0 ... \n",
- "102804 youngch03 2016 1 KCA AL 34 1 0 0 0 ... \n",
- "102805 youngch04 2016 1 BOS AL 76 203 29 56 18 ... \n",
- "102806 younger03 2016 1 NYA AL 6 1 2 0 0 ... \n",
- "102807 youngma03 2016 1 ATL NL 8 0 0 0 0 ... \n",
- "102808 zastrro01 2016 1 CHN NL 8 3 0 0 0 ... \n",
- "102809 zieglbr01 2016 1 ARI NL 36 0 0 0 0 ... \n",
- "102810 zieglbr01 2016 2 BOS AL 33 0 0 0 0 ... \n",
- "102811 zimmejo02 2016 1 DET AL 19 4 0 1 0 ... \n",
- "102812 zimmery01 2016 1 WAS NL 115 427 60 93 18 ... \n",
- "102813 zobribe01 2016 1 CHN NL 147 523 94 142 31 ... \n",
- "102814 zuninmi01 2016 1 SEA AL 55 164 16 34 7 ... \n",
- "102815 zychto01 2016 1 SEA AL 12 0 0 0 0 ... \n",
- "\n",
- " RBI SB CS BB SO IBB HBP SH SF GIDP \n",
- "102801 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "102802 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "102803 0.0 0.0 0.0 0 2.0 0.0 0.0 0.0 0.0 0.0 \n",
- "102804 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "102805 24.0 4.0 2.0 21 50.0 0.0 3.0 0.0 0.0 4.0 \n",
- "102806 0.0 1.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "102807 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "102808 0.0 0.0 0.0 0 2.0 0.0 0.0 0.0 0.0 0.0 \n",
- "102809 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "102810 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "102811 0.0 0.0 0.0 0 2.0 0.0 0.0 1.0 0.0 0.0 \n",
- "102812 46.0 4.0 1.0 29 104.0 1.0 5.0 0.0 6.0 12.0 \n",
- "102813 76.0 6.0 4.0 96 82.0 6.0 4.0 4.0 4.0 17.0 \n",
- "102814 31.0 0.0 0.0 21 65.0 0.0 6.0 0.0 1.0 0.0 \n",
- "102815 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "\n",
- "[15 rows x 22 columns]"
- ]
- },
- "execution_count": 3,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df[-15:]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "df[\"RBI\"][:5]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "df[[\"RBI\", \"G\"]][:10]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Selecting data by `.` selector on column and index name\n",
- "\n",
- "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:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0 0.0\n",
- "1 13.0\n",
- "2 19.0\n",
- "3 27.0\n",
- "4 16.0\n",
- "5 5.0\n",
- "6 2.0\n",
- "7 34.0\n",
- "8 1.0\n",
- "9 11.0\n",
- "Name: RBI, dtype: float64"
- ]
- },
- "execution_count": 4,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df.RBI[:10]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Similarly, we can pass a **list** of the columns we'd like, so let's get the `RBI` and `G` (games played) data:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>RBI</th>\n",
- " <th>G</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>0</th>\n",
- " <td>0.0</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>13.0</td>\n",
- " <td>25</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2</th>\n",
- " <td>19.0</td>\n",
- " <td>29</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>3</th>\n",
- " <td>27.0</td>\n",
- " <td>27</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>4</th>\n",
- " <td>16.0</td>\n",
- " <td>25</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>5</th>\n",
- " <td>5.0</td>\n",
- " <td>12</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>6</th>\n",
- " <td>2.0</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>7</th>\n",
- " <td>34.0</td>\n",
- " <td>31</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>8</th>\n",
- " <td>1.0</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>9</th>\n",
- " <td>11.0</td>\n",
- " <td>18</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " RBI G\n",
- "0 0.0 1\n",
- "1 13.0 25\n",
- "2 19.0 29\n",
- "3 27.0 27\n",
- "4 16.0 25\n",
- "5 5.0 12\n",
- "6 2.0 1\n",
- "7 34.0 31\n",
- "8 1.0 1\n",
- "9 11.0 18"
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df[[\"RBI\", \"G\"]][:10]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Boolean selecting\n",
- "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",
- "\n",
- "Say we want to find all items in our DataFrame where `yearID` is `2015` or\n",
- "\n",
- "```\n",
- "df.yearID == 2015\n",
- "```\n",
- "\n",
- "Let's first see what this does."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "0 False\n",
- "1 False\n",
- "2 False\n",
- "3 False\n",
- "4 False\n",
- "5 False\n",
- "6 False\n",
- "7 False\n",
- "8 False\n",
- "9 False\n",
- "10 False\n",
- "11 False\n",
- "12 False\n",
- "13 False\n",
- "14 False\n",
- "15 False\n",
- "16 False\n",
- "17 False\n",
- "18 False\n",
- "19 False\n",
- "20 False\n",
- "21 False\n",
- "22 False\n",
- "23 False\n",
- "24 False\n",
- "25 False\n",
- "26 False\n",
- "27 False\n",
- "28 False\n",
- "29 False\n",
- " ... \n",
- "102786 False\n",
- "102787 False\n",
- "102788 False\n",
- "102789 False\n",
- "102790 False\n",
- "102791 False\n",
- "102792 False\n",
- "102793 False\n",
- "102794 False\n",
- "102795 False\n",
- "102796 False\n",
- "102797 False\n",
- "102798 False\n",
- "102799 False\n",
- "102800 False\n",
- "102801 False\n",
- "102802 False\n",
- "102803 False\n",
- "102804 False\n",
- "102805 False\n",
- "102806 False\n",
- "102807 False\n",
- "102808 False\n",
- "102809 False\n",
- "102810 False\n",
- "102811 False\n",
- "102812 False\n",
- "102813 False\n",
- "102814 False\n",
- "102815 False\n",
- "Name: yearID, Length: 102816, dtype: bool"
- ]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df.yearID == 2015"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>yearID</th>\n",
- " <th>stint</th>\n",
- " <th>teamID</th>\n",
- " <th>lgID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>R</th>\n",
- " <th>H</th>\n",
- " <th>2B</th>\n",
- " <th>...</th>\n",
- " <th>RBI</th>\n",
- " <th>SB</th>\n",
- " <th>CS</th>\n",
- " <th>BB</th>\n",
- " <th>SO</th>\n",
- " <th>IBB</th>\n",
- " <th>HBP</th>\n",
- " <th>SH</th>\n",
- " <th>SF</th>\n",
- " <th>GIDP</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>99847</th>\n",
- " <td>aardsda01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>ATL</td>\n",
- " <td>NL</td>\n",
- " <td>33</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99848</th>\n",
- " <td>abadfe01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>OAK</td>\n",
- " <td>AL</td>\n",
- " <td>62</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99849</th>\n",
- " <td>abreujo02</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>CHA</td>\n",
- " <td>AL</td>\n",
- " <td>154</td>\n",
- " <td>613</td>\n",
- " <td>88</td>\n",
- " <td>178</td>\n",
- " <td>34</td>\n",
- " <td>...</td>\n",
- " <td>101.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>39</td>\n",
- " <td>140.0</td>\n",
- " <td>11.0</td>\n",
- " <td>15.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>16.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99850</th>\n",
- " <td>achteaj01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>MIN</td>\n",
- " <td>AL</td>\n",
- " <td>11</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99851</th>\n",
- " <td>ackledu01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>SEA</td>\n",
- " <td>AL</td>\n",
- " <td>85</td>\n",
- " <td>186</td>\n",
- " <td>22</td>\n",
- " <td>40</td>\n",
- " <td>8</td>\n",
- " <td>...</td>\n",
- " <td>19.0</td>\n",
- " <td>2.0</td>\n",
- " <td>2.0</td>\n",
- " <td>14</td>\n",
- " <td>38.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>3.0</td>\n",
- " <td>3.0</td>\n",
- " <td>3.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99852</th>\n",
- " <td>ackledu01</td>\n",
- " <td>2015</td>\n",
- " <td>2</td>\n",
- " <td>NYA</td>\n",
- " <td>AL</td>\n",
- " <td>23</td>\n",
- " <td>52</td>\n",
- " <td>6</td>\n",
- " <td>15</td>\n",
- " <td>3</td>\n",
- " <td>...</td>\n",
- " <td>11.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>4</td>\n",
- " <td>7.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99853</th>\n",
- " <td>adamecr01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>COL</td>\n",
- " <td>NL</td>\n",
- " <td>26</td>\n",
- " <td>53</td>\n",
- " <td>4</td>\n",
- " <td>13</td>\n",
- " <td>1</td>\n",
- " <td>...</td>\n",
- " <td>3.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>3</td>\n",
- " <td>11.0</td>\n",
- " <td>1.0</td>\n",
- " <td>1.0</td>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99854</th>\n",
- " <td>adamsau01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>CLE</td>\n",
- " <td>AL</td>\n",
- " <td>28</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99855</th>\n",
- " <td>adamsma01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>SLN</td>\n",
- " <td>NL</td>\n",
- " <td>60</td>\n",
- " <td>175</td>\n",
- " <td>14</td>\n",
- " <td>42</td>\n",
- " <td>9</td>\n",
- " <td>...</td>\n",
- " <td>24.0</td>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " <td>10</td>\n",
- " <td>41.0</td>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>1.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99856</th>\n",
- " <td>adcocna01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>CIN</td>\n",
- " <td>NL</td>\n",
- " <td>13</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "<p>10 rows × 22 columns</p>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID yearID stint teamID lgID G AB R H 2B ... \\\n",
- "99847 aardsda01 2015 1 ATL NL 33 1 0 0 0 ... \n",
- "99848 abadfe01 2015 1 OAK AL 62 0 0 0 0 ... \n",
- "99849 abreujo02 2015 1 CHA AL 154 613 88 178 34 ... \n",
- "99850 achteaj01 2015 1 MIN AL 11 0 0 0 0 ... \n",
- "99851 ackledu01 2015 1 SEA AL 85 186 22 40 8 ... \n",
- "99852 ackledu01 2015 2 NYA AL 23 52 6 15 3 ... \n",
- "99853 adamecr01 2015 1 COL NL 26 53 4 13 1 ... \n",
- "99854 adamsau01 2015 1 CLE AL 28 1 0 0 0 ... \n",
- "99855 adamsma01 2015 1 SLN NL 60 175 14 42 9 ... \n",
- "99856 adcocna01 2015 1 CIN NL 13 0 0 0 0 ... \n",
- "\n",
- " RBI SB CS BB SO IBB HBP SH SF GIDP \n",
- "99847 0.0 0.0 0.0 0 1.0 0.0 0.0 0.0 0.0 0.0 \n",
- "99848 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "99849 101.0 0.0 0.0 39 140.0 11.0 15.0 0.0 1.0 16.0 \n",
- "99850 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "99851 19.0 2.0 2.0 14 38.0 0.0 1.0 3.0 3.0 3.0 \n",
- "99852 11.0 0.0 0.0 4 7.0 0.0 0.0 0.0 1.0 0.0 \n",
- "99853 3.0 0.0 1.0 3 11.0 1.0 1.0 1.0 0.0 0.0 \n",
- "99854 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 1.0 \n",
- "99855 24.0 1.0 0.0 10 41.0 1.0 0.0 0.0 1.0 1.0 \n",
- "99856 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "\n",
- "[10 rows x 22 columns]"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df.loc[df.yearID == 2015][:10] # note we're restricting the return to just the first 10 values"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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",
- "\n",
- "```\n",
- "df.yearID == 2015\n",
- "AND\n",
- "df.teamID == \"MIN\"\n",
- "```\n",
- "\n",
- "We simply put these in parethesis and use the `&` operator."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>yearID</th>\n",
- " <th>stint</th>\n",
- " <th>teamID</th>\n",
- " <th>lgID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>R</th>\n",
- " <th>H</th>\n",
- " <th>2B</th>\n",
- " <th>...</th>\n",
- " <th>RBI</th>\n",
- " <th>SB</th>\n",
- " <th>CS</th>\n",
- " <th>BB</th>\n",
- " <th>SO</th>\n",
- " <th>IBB</th>\n",
- " <th>HBP</th>\n",
- " <th>SH</th>\n",
- " <th>SF</th>\n",
- " <th>GIDP</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>99850</th>\n",
- " <td>achteaj01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>MIN</td>\n",
- " <td>AL</td>\n",
- " <td>11</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99891</th>\n",
- " <td>arciaos01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>MIN</td>\n",
- " <td>AL</td>\n",
- " <td>19</td>\n",
- " <td>58</td>\n",
- " <td>6</td>\n",
- " <td>16</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>8.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>4</td>\n",
- " <td>15.0</td>\n",
- " <td>4.0</td>\n",
- " <td>2.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>2.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99954</th>\n",
- " <td>bernido01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>MIN</td>\n",
- " <td>AL</td>\n",
- " <td>4</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>1</td>\n",
- " <td>1</td>\n",
- " <td>...</td>\n",
- " <td>2.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1</td>\n",
- " <td>3.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99988</th>\n",
- " <td>boyerbl01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>MIN</td>\n",
- " <td>AL</td>\n",
- " <td>68</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100030</th>\n",
- " <td>buxtoby01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>MIN</td>\n",
- " <td>AL</td>\n",
- " <td>46</td>\n",
- " <td>129</td>\n",
- " <td>16</td>\n",
- " <td>27</td>\n",
- " <td>7</td>\n",
- " <td>...</td>\n",
- " <td>6.0</td>\n",
- " <td>2.0</td>\n",
- " <td>2.0</td>\n",
- " <td>6</td>\n",
- " <td>44.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>2.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100139</th>\n",
- " <td>cottsne01</td>\n",
- " <td>2015</td>\n",
- " <td>2</td>\n",
- " <td>MIN</td>\n",
- " <td>AL</td>\n",
- " <td>17</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>MIN</td>\n",
- " <td>AL</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>101</td>\n",
- " <td>148</td>\n",
- " <td>39</td>\n",
- " <td>...</td>\n",
- " <td>77.0</td>\n",
- " <td>12.0</td>\n",
- " <td>4.0</td>\n",
- " <td>61</td>\n",
- " <td>148.0</td>\n",
- " <td>2.0</td>\n",
- " <td>7.0</td>\n",
- " <td>0.0</td>\n",
- " <td>8.0</td>\n",
- " <td>10.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100221</th>\n",
- " <td>duensbr01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>MIN</td>\n",
- " <td>AL</td>\n",
- " <td>55</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100222</th>\n",
- " <td>duffety01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>MIN</td>\n",
- " <td>AL</td>\n",
- " <td>10</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100249</th>\n",
- " <td>escobed01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>MIN</td>\n",
- " <td>AL</td>\n",
- " <td>127</td>\n",
- " <td>409</td>\n",
- " <td>48</td>\n",
- " <td>107</td>\n",
- " <td>31</td>\n",
- " <td>...</td>\n",
- " <td>58.0</td>\n",
- " <td>2.0</td>\n",
- " <td>3.0</td>\n",
- " <td>28</td>\n",
- " <td>86.0</td>\n",
- " <td>1.0</td>\n",
- " <td>2.0</td>\n",
- " <td>2.0</td>\n",
- " <td>5.0</td>\n",
- " <td>7.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "<p>10 rows × 22 columns</p>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID yearID stint teamID lgID G AB R H 2B ... \\\n",
- "99850 achteaj01 2015 1 MIN AL 11 0 0 0 0 ... \n",
- "99891 arciaos01 2015 1 MIN AL 19 58 6 16 0 ... \n",
- "99954 bernido01 2015 1 MIN AL 4 5 1 1 1 ... \n",
- "99988 boyerbl01 2015 1 MIN AL 68 0 0 0 0 ... \n",
- "100030 buxtoby01 2015 1 MIN AL 46 129 16 27 7 ... \n",
- "100139 cottsne01 2015 2 MIN AL 17 0 0 0 0 ... \n",
- "100215 doziebr01 2015 1 MIN AL 157 628 101 148 39 ... \n",
- "100221 duensbr01 2015 1 MIN AL 55 1 0 0 0 ... \n",
- "100222 duffety01 2015 1 MIN AL 10 0 0 0 0 ... \n",
- "100249 escobed01 2015 1 MIN AL 127 409 48 107 31 ... \n",
- "\n",
- " RBI SB CS BB SO IBB HBP SH SF GIDP \n",
- "99850 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "99891 8.0 0.0 0.0 4 15.0 4.0 2.0 0.0 1.0 2.0 \n",
- "99954 2.0 0.0 0.0 1 3.0 0.0 0.0 0.0 0.0 0.0 \n",
- "99988 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "100030 6.0 2.0 2.0 6 44.0 0.0 1.0 2.0 0.0 1.0 \n",
- "100139 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "100215 77.0 12.0 4.0 61 148.0 2.0 7.0 0.0 8.0 10.0 \n",
- "100221 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "100222 0.0 0.0 0.0 0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
- "100249 58.0 2.0 3.0 28 86.0 1.0 2.0 2.0 5.0 7.0 \n",
- "\n",
- "[10 rows x 22 columns]"
- ]
- },
- "execution_count": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df.loc[(df.yearID == 2015) & (df.teamID == \"MIN\")].head(10)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>99850</th>\n",
- " <td>achteaj01</td>\n",
- " <td>11</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99891</th>\n",
- " <td>arciaos01</td>\n",
- " <td>19</td>\n",
- " <td>58</td>\n",
- " <td>16</td>\n",
- " <td>2</td>\n",
- " <td>8.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99954</th>\n",
- " <td>bernido01</td>\n",
- " <td>4</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>2.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99988</th>\n",
- " <td>boyerbl01</td>\n",
- " <td>68</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100030</th>\n",
- " <td>buxtoby01</td>\n",
- " <td>46</td>\n",
- " <td>129</td>\n",
- " <td>27</td>\n",
- " <td>2</td>\n",
- " <td>6.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100139</th>\n",
- " <td>cottsne01</td>\n",
- " <td>17</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100221</th>\n",
- " <td>duensbr01</td>\n",
- " <td>55</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100222</th>\n",
- " <td>duffety01</td>\n",
- " <td>10</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100249</th>\n",
- " <td>escobed01</td>\n",
- " <td>127</td>\n",
- " <td>409</td>\n",
- " <td>107</td>\n",
- " <td>12</td>\n",
- " <td>58.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100270</th>\n",
- " <td>fienca01</td>\n",
- " <td>62</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100302</th>\n",
- " <td>fryerer01</td>\n",
- " <td>15</td>\n",
- " <td>22</td>\n",
- " <td>5</td>\n",
- " <td>0</td>\n",
- " <td>2.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100333</th>\n",
- " <td>gibsoky01</td>\n",
- " <td>32</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100373</th>\n",
- " <td>grahajr01</td>\n",
- " <td>39</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100455</th>\n",
- " <td>herrmch01</td>\n",
- " <td>45</td>\n",
- " <td>103</td>\n",
- " <td>15</td>\n",
- " <td>2</td>\n",
- " <td>10.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100459</th>\n",
- " <td>hicksaa01</td>\n",
- " <td>97</td>\n",
- " <td>352</td>\n",
- " <td>90</td>\n",
- " <td>11</td>\n",
- " <td>33.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100486</th>\n",
- " <td>hugheph01</td>\n",
- " <td>27</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>139</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100521</th>\n",
- " <td>jepseke01</td>\n",
- " <td>29</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100564</th>\n",
- " <td>keplema01</td>\n",
- " <td>3</td>\n",
- " <td>7</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>158</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100701</th>\n",
- " <td>maytr01</td>\n",
- " <td>48</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100729</th>\n",
- " <td>meyeral01</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100737</th>\n",
- " <td>milonto01</td>\n",
- " <td>24</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100807</th>\n",
- " <td>nolasri01</td>\n",
- " <td>9</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100816</th>\n",
- " <td>nunezed02</td>\n",
- " <td>72</td>\n",
- " <td>188</td>\n",
- " <td>53</td>\n",
- " <td>4</td>\n",
- " <td>20.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100837</th>\n",
- " <td>orourry01</td>\n",
- " <td>28</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100872</th>\n",
- " <td>pelfrmi01</td>\n",
- " <td>30</td>\n",
- " <td>3</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100895</th>\n",
- " <td>perkigl01</td>\n",
- " <td>60</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>152</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100917</th>\n",
- " <td>polanjo01</td>\n",
- " <td>4</td>\n",
- " <td>10</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>1.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100925</th>\n",
- " <td>pressry01</td>\n",
- " <td>27</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100994</th>\n",
- " <td>robinsh01</td>\n",
- " <td>83</td>\n",
- " <td>180</td>\n",
- " <td>45</td>\n",
- " <td>0</td>\n",
- " <td>16.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101023</th>\n",
- " <td>rosared01</td>\n",
- " <td>122</td>\n",
- " <td>453</td>\n",
- " <td>121</td>\n",
- " <td>13</td>\n",
- " <td>50.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101067</th>\n",
- " <td>sanomi01</td>\n",
- " <td>80</td>\n",
- " <td>279</td>\n",
- " <td>75</td>\n",
- " <td>18</td>\n",
- " <td>52.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101069</th>\n",
- " <td>santada01</td>\n",
- " <td>91</td>\n",
- " <td>261</td>\n",
- " <td>56</td>\n",
- " <td>0</td>\n",
- " <td>21.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101072</th>\n",
- " <td>santaer01</td>\n",
- " <td>17</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101079</th>\n",
- " <td>schafjo02</td>\n",
- " <td>27</td>\n",
- " <td>69</td>\n",
- " <td>15</td>\n",
- " <td>0</td>\n",
- " <td>5.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101144</th>\n",
- " <td>staufti01</td>\n",
- " <td>13</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>131</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101189</th>\n",
- " <td>thielca01</td>\n",
- " <td>6</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101193</th>\n",
- " <td>thompaa01</td>\n",
- " <td>41</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101203</th>\n",
- " <td>tonkimi01</td>\n",
- " <td>26</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101240</th>\n",
- " <td>vargake01</td>\n",
- " <td>58</td>\n",
- " <td>175</td>\n",
- " <td>42</td>\n",
- " <td>5</td>\n",
- " <td>17.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI\n",
- "99850 achteaj01 11 0 0 0 0.0\n",
- "99891 arciaos01 19 58 16 2 8.0\n",
- "99954 bernido01 4 5 1 0 2.0\n",
- "99988 boyerbl01 68 0 0 0 0.0\n",
- "100030 buxtoby01 46 129 27 2 6.0\n",
- "100139 cottsne01 17 0 0 0 0.0\n",
- "100215 doziebr01 157 628 148 28 77.0\n",
- "100221 duensbr01 55 1 0 0 0.0\n",
- "100222 duffety01 10 0 0 0 0.0\n",
- "100249 escobed01 127 409 107 12 58.0\n",
- "100270 fienca01 62 0 0 0 0.0\n",
- "100302 fryerer01 15 22 5 0 2.0\n",
- "100333 gibsoky01 32 5 1 0 0.0\n",
- "100373 grahajr01 39 0 0 0 0.0\n",
- "100455 herrmch01 45 103 15 2 10.0\n",
- "100459 hicksaa01 97 352 90 11 33.0\n",
- "100486 hugheph01 27 3 0 0 0.0\n",
- "100488 hunteto01 139 521 125 22 81.0\n",
- "100521 jepseke01 29 0 0 0 0.0\n",
- "100564 keplema01 3 7 1 0 0.0\n",
- "100696 mauerjo01 158 592 157 10 66.0\n",
- "100701 maytr01 48 3 0 0 0.0\n",
- "100729 meyeral01 2 0 0 0 0.0\n",
- "100737 milonto01 24 2 0 0 0.0\n",
- "100807 nolasri01 9 3 0 0 0.0\n",
- "100816 nunezed02 72 188 53 4 20.0\n",
- "100837 orourry01 28 0 0 0 0.0\n",
- "100872 pelfrmi01 30 3 2 0 0.0\n",
- "100895 perkigl01 60 0 0 0 0.0\n",
- "100915 plouftr01 152 573 140 22 86.0\n",
- "100917 polanjo01 4 10 3 0 1.0\n",
- "100925 pressry01 27 0 0 0 0.0\n",
- "100994 robinsh01 83 180 45 0 16.0\n",
- "101023 rosared01 122 453 121 13 50.0\n",
- "101067 sanomi01 80 279 75 18 52.0\n",
- "101069 santada01 91 261 56 0 21.0\n",
- "101072 santaer01 17 0 0 0 0.0\n",
- "101079 schafjo02 27 69 15 0 5.0\n",
- "101144 staufti01 13 0 0 0 0.0\n",
- "101164 suzukku01 131 433 104 5 50.0\n",
- "101189 thielca01 6 0 0 0 0.0\n",
- "101193 thompaa01 41 0 0 0 0.0\n",
- "101203 tonkimi01 26 0 0 0 0.0\n",
- "101240 vargake01 58 175 42 5 17.0"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df.loc[(df.yearID == 2015) & (df.teamID == \"MIN\"),\\\n",
- " ['playerID', 'G', 'AB', 'H', 'HR', 'RBI']]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Sorting"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>158</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>152</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>139</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>131</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100249</th>\n",
- " <td>escobed01</td>\n",
- " <td>127</td>\n",
- " <td>409</td>\n",
- " <td>107</td>\n",
- " <td>12</td>\n",
- " <td>58.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101023</th>\n",
- " <td>rosared01</td>\n",
- " <td>122</td>\n",
- " <td>453</td>\n",
- " <td>121</td>\n",
- " <td>13</td>\n",
- " <td>50.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100459</th>\n",
- " <td>hicksaa01</td>\n",
- " <td>97</td>\n",
- " <td>352</td>\n",
- " <td>90</td>\n",
- " <td>11</td>\n",
- " <td>33.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101069</th>\n",
- " <td>santada01</td>\n",
- " <td>91</td>\n",
- " <td>261</td>\n",
- " <td>56</td>\n",
- " <td>0</td>\n",
- " <td>21.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100994</th>\n",
- " <td>robinsh01</td>\n",
- " <td>83</td>\n",
- " <td>180</td>\n",
- " <td>45</td>\n",
- " <td>0</td>\n",
- " <td>16.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101067</th>\n",
- " <td>sanomi01</td>\n",
- " <td>80</td>\n",
- " <td>279</td>\n",
- " <td>75</td>\n",
- " <td>18</td>\n",
- " <td>52.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100816</th>\n",
- " <td>nunezed02</td>\n",
- " <td>72</td>\n",
- " <td>188</td>\n",
- " <td>53</td>\n",
- " <td>4</td>\n",
- " <td>20.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99988</th>\n",
- " <td>boyerbl01</td>\n",
- " <td>68</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100270</th>\n",
- " <td>fienca01</td>\n",
- " <td>62</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100895</th>\n",
- " <td>perkigl01</td>\n",
- " <td>60</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101240</th>\n",
- " <td>vargake01</td>\n",
- " <td>58</td>\n",
- " <td>175</td>\n",
- " <td>42</td>\n",
- " <td>5</td>\n",
- " <td>17.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100221</th>\n",
- " <td>duensbr01</td>\n",
- " <td>55</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100701</th>\n",
- " <td>maytr01</td>\n",
- " <td>48</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100030</th>\n",
- " <td>buxtoby01</td>\n",
- " <td>46</td>\n",
- " <td>129</td>\n",
- " <td>27</td>\n",
- " <td>2</td>\n",
- " <td>6.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100455</th>\n",
- " <td>herrmch01</td>\n",
- " <td>45</td>\n",
- " <td>103</td>\n",
- " <td>15</td>\n",
- " <td>2</td>\n",
- " <td>10.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI\n",
- "100696 mauerjo01 158 592 157 10 66.0\n",
- "100215 doziebr01 157 628 148 28 77.0\n",
- "100915 plouftr01 152 573 140 22 86.0\n",
- "100488 hunteto01 139 521 125 22 81.0\n",
- "101164 suzukku01 131 433 104 5 50.0\n",
- "100249 escobed01 127 409 107 12 58.0\n",
- "101023 rosared01 122 453 121 13 50.0\n",
- "100459 hicksaa01 97 352 90 11 33.0\n",
- "101069 santada01 91 261 56 0 21.0\n",
- "100994 robinsh01 83 180 45 0 16.0\n",
- "101067 sanomi01 80 279 75 18 52.0\n",
- "100816 nunezed02 72 188 53 4 20.0\n",
- "99988 boyerbl01 68 0 0 0 0.0\n",
- "100270 fienca01 62 0 0 0 0.0\n",
- "100895 perkigl01 60 0 0 0 0.0\n",
- "101240 vargake01 58 175 42 5 17.0\n",
- "100221 duensbr01 55 1 0 0 0.0\n",
- "100701 maytr01 48 3 0 0 0.0\n",
- "100030 buxtoby01 46 129 27 2 6.0\n",
- "100455 herrmch01 45 103 15 2 10.0"
- ]
- },
- "execution_count": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015 = df.loc[(df.yearID == 2015) & (df.teamID == \"MIN\"),\\\n",
- " ['playerID', 'G', 'AB', 'H', 'HR', 'RBI']]\\\n",
- " .sort_values('G', ascending=False)\n",
- "df_min_2015.head(20)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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,"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>101189</th>\n",
- " <td>thielca01</td>\n",
- " <td>6</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99954</th>\n",
- " <td>bernido01</td>\n",
- " <td>4</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>2.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100917</th>\n",
- " <td>polanjo01</td>\n",
- " <td>4</td>\n",
- " <td>10</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>1.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100564</th>\n",
- " <td>keplema01</td>\n",
- " <td>3</td>\n",
- " <td>7</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100729</th>\n",
- " <td>meyeral01</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI\n",
- "101189 thielca01 6 0 0 0 0.0\n",
- "99954 bernido01 4 5 1 0 2.0\n",
- "100917 polanjo01 4 10 3 0 1.0\n",
- "100564 keplema01 3 7 1 0 0.0\n",
- "100729 meyeral01 2 0 0 0 0.0"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df.loc[(df.yearID == 2015) & (df.teamID == \"MIN\"),\\\n",
- " ['playerID', 'G', 'AB', 'H', 'HR', 'RBI']]\\\n",
- " .sort_values(['G', 'HR'], ascending=False).tail()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## DataFrame manipulation"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Adding and dropping columns"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " <th>HtoAB</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>158</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66.0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77.0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>152</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86.0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>139</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81.0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>131</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50.0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI HtoAB\n",
- "100696 mauerjo01 158 592 157 10 66.0 0\n",
- "100215 doziebr01 157 628 148 28 77.0 0\n",
- "100915 plouftr01 152 573 140 22 86.0 0\n",
- "100488 hunteto01 139 521 125 22 81.0 0\n",
- "101164 suzukku01 131 433 104 5 50.0 0"
- ]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015.loc[:,'HtoAB'] = 0\n",
- "df_min_2015.head()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>158</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>152</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>139</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>131</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI\n",
- "100696 mauerjo01 158 592 157 10 66.0\n",
- "100215 doziebr01 157 628 148 28 77.0\n",
- "100915 plouftr01 152 573 140 22 86.0\n",
- "100488 hunteto01 139 521 125 22 81.0\n",
- "101164 suzukku01 131 433 104 5 50.0"
- ]
- },
- "execution_count": 13,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015 = df_min_2015.drop('HtoAB', axis=1)\n",
- "df_min_2015.head()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "100696 157\n",
- "100215 148\n",
- "100915 140\n",
- "100488 125\n",
- "101164 104\n",
- "100249 107\n",
- "101023 121\n",
- "100459 90\n",
- "101069 56\n",
- "100994 45\n",
- "Name: H, dtype: int64"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015.H.head(10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "df_min_2015.loc[:,'HtoAB'] = 0\n",
- "df_min_2015.loc[:,'HtoAB'] = [v.H/v.AB \n",
- " if v.AB > 0 else 0 \n",
- " for r, v in df_min_2015.iterrows()]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " <th>HtoAB</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>158</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66.0</td>\n",
- " <td>0.265203</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77.0</td>\n",
- " <td>0.235669</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>152</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86.0</td>\n",
- " <td>0.244328</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>139</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81.0</td>\n",
- " <td>0.239923</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>131</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50.0</td>\n",
- " <td>0.240185</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100249</th>\n",
- " <td>escobed01</td>\n",
- " <td>127</td>\n",
- " <td>409</td>\n",
- " <td>107</td>\n",
- " <td>12</td>\n",
- " <td>58.0</td>\n",
- " <td>0.261614</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101023</th>\n",
- " <td>rosared01</td>\n",
- " <td>122</td>\n",
- " <td>453</td>\n",
- " <td>121</td>\n",
- " <td>13</td>\n",
- " <td>50.0</td>\n",
- " <td>0.267108</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100459</th>\n",
- " <td>hicksaa01</td>\n",
- " <td>97</td>\n",
- " <td>352</td>\n",
- " <td>90</td>\n",
- " <td>11</td>\n",
- " <td>33.0</td>\n",
- " <td>0.255682</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101069</th>\n",
- " <td>santada01</td>\n",
- " <td>91</td>\n",
- " <td>261</td>\n",
- " <td>56</td>\n",
- " <td>0</td>\n",
- " <td>21.0</td>\n",
- " <td>0.214559</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100994</th>\n",
- " <td>robinsh01</td>\n",
- " <td>83</td>\n",
- " <td>180</td>\n",
- " <td>45</td>\n",
- " <td>0</td>\n",
- " <td>16.0</td>\n",
- " <td>0.250000</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI HtoAB\n",
- "100696 mauerjo01 158 592 157 10 66.0 0.265203\n",
- "100215 doziebr01 157 628 148 28 77.0 0.235669\n",
- "100915 plouftr01 152 573 140 22 86.0 0.244328\n",
- "100488 hunteto01 139 521 125 22 81.0 0.239923\n",
- "101164 suzukku01 131 433 104 5 50.0 0.240185\n",
- "100249 escobed01 127 409 107 12 58.0 0.261614\n",
- "101023 rosared01 122 453 121 13 50.0 0.267108\n",
- "100459 hicksaa01 97 352 90 11 33.0 0.255682\n",
- "101069 santada01 91 261 56 0 21.0 0.214559\n",
- "100994 robinsh01 83 180 45 0 16.0 0.250000"
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015.head(10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " <th>HtoAB</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>101023</th>\n",
- " <td>rosared01</td>\n",
- " <td>122</td>\n",
- " <td>453</td>\n",
- " <td>121</td>\n",
- " <td>13</td>\n",
- " <td>50.0</td>\n",
- " <td>0.267108</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>158</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66.0</td>\n",
- " <td>0.265203</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100249</th>\n",
- " <td>escobed01</td>\n",
- " <td>127</td>\n",
- " <td>409</td>\n",
- " <td>107</td>\n",
- " <td>12</td>\n",
- " <td>58.0</td>\n",
- " <td>0.261614</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100459</th>\n",
- " <td>hicksaa01</td>\n",
- " <td>97</td>\n",
- " <td>352</td>\n",
- " <td>90</td>\n",
- " <td>11</td>\n",
- " <td>33.0</td>\n",
- " <td>0.255682</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100994</th>\n",
- " <td>robinsh01</td>\n",
- " <td>83</td>\n",
- " <td>180</td>\n",
- " <td>45</td>\n",
- " <td>0</td>\n",
- " <td>16.0</td>\n",
- " <td>0.250000</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>152</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86.0</td>\n",
- " <td>0.244328</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>131</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50.0</td>\n",
- " <td>0.240185</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>139</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81.0</td>\n",
- " <td>0.239923</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77.0</td>\n",
- " <td>0.235669</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101069</th>\n",
- " <td>santada01</td>\n",
- " <td>91</td>\n",
- " <td>261</td>\n",
- " <td>56</td>\n",
- " <td>0</td>\n",
- " <td>21.0</td>\n",
- " <td>0.214559</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI HtoAB\n",
- "101023 rosared01 122 453 121 13 50.0 0.267108\n",
- "100696 mauerjo01 158 592 157 10 66.0 0.265203\n",
- "100249 escobed01 127 409 107 12 58.0 0.261614\n",
- "100459 hicksaa01 97 352 90 11 33.0 0.255682\n",
- "100994 robinsh01 83 180 45 0 16.0 0.250000\n",
- "100915 plouftr01 152 573 140 22 86.0 0.244328\n",
- "101164 suzukku01 131 433 104 5 50.0 0.240185\n",
- "100488 hunteto01 139 521 125 22 81.0 0.239923\n",
- "100215 doziebr01 157 628 148 28 77.0 0.235669\n",
- "101069 santada01 91 261 56 0 21.0 0.214559"
- ]
- },
- "execution_count": 17,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015[df_min_2015.G>80].sort_values('HtoAB', ascending=False)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>HtoAB</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " <th>G</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>0.265203</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66.0</td>\n",
- " <td>158</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>0.235669</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77.0</td>\n",
- " <td>157</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>0.244328</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86.0</td>\n",
- " <td>152</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>0.239923</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81.0</td>\n",
- " <td>139</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>0.240185</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50.0</td>\n",
- " <td>131</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID HtoAB AB H HR RBI G\n",
- "100696 mauerjo01 0.265203 592 157 10 66.0 158\n",
- "100215 doziebr01 0.235669 628 148 28 77.0 157\n",
- "100915 plouftr01 0.244328 573 140 22 86.0 152\n",
- "100488 hunteto01 0.239923 521 125 22 81.0 139\n",
- "101164 suzukku01 0.240185 433 104 5 50.0 131"
- ]
- },
- "execution_count": 18,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015 = df_min_2015.reindex(columns=['playerID', 'HtoAB', 'AB', 'H', 'HR', 'RBI', 'G'])\n",
- "df_min_2015.head()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>158</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>152</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>139</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>131</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI\n",
- "100696 mauerjo01 158 592 157 10 66.0\n",
- "100215 doziebr01 157 628 148 28 77.0\n",
- "100915 plouftr01 152 573 140 22 86.0\n",
- "100488 hunteto01 139 521 125 22 81.0\n",
- "101164 suzukku01 131 433 104 5 50.0"
- ]
- },
- "execution_count": 19,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015 = df_min_2015.reindex(columns=['playerID', 'G', 'AB', 'H', 'HR', 'RBI'])\n",
- "df_min_2015.head()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Adding and dropping rows"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Adding rows can be achieved using `loc[]` and setting the new index to a dictionary of values using the column labels as keys."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>100917</th>\n",
- " <td>polanjo01</td>\n",
- " <td>4</td>\n",
- " <td>10</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99954</th>\n",
- " <td>bernido01</td>\n",
- " <td>4</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>2</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100564</th>\n",
- " <td>keplema01</td>\n",
- " <td>3</td>\n",
- " <td>7</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100729</th>\n",
- " <td>meyeral01</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>200000</th>\n",
- " <td>keith01</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI\n",
- "100917 polanjo01 4 10 3 0 1\n",
- "99954 bernido01 4 5 1 0 2\n",
- "100564 keplema01 3 7 1 0 0\n",
- "100729 meyeral01 2 0 0 0 0\n",
- "200000 keith01 0 0 0 0 0"
- ]
- },
- "execution_count": 20,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015.loc[200000] = \\\n",
- " { 'playerID': 'keith01',\n",
- " 'RBI': '0',\n",
- " 'G': '0',\n",
- " 'H': '0',\n",
- " 'HR': '0',\n",
- " 'AB': '0' }\n",
- " \n",
- "df_min_2015.tail()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "It is also the same with lists and tuples."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>99954</th>\n",
- " <td>bernido01</td>\n",
- " <td>4</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>2</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100564</th>\n",
- " <td>keplema01</td>\n",
- " <td>3</td>\n",
- " <td>7</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100729</th>\n",
- " <td>meyeral01</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>200000</th>\n",
- " <td>keith01</td>\n",
- " <td>1</td>\n",
- " <td>1</td>\n",
- " <td>1</td>\n",
- " <td>1</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>200001</th>\n",
- " <td>keith02</td>\n",
- " <td>1</td>\n",
- " <td>1</td>\n",
- " <td>1</td>\n",
- " <td>1</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI\n",
- "99954 bernido01 4 5 1 0 2\n",
- "100564 keplema01 3 7 1 0 0\n",
- "100729 meyeral01 2 0 0 0 0\n",
- "200000 keith01 1 1 1 1 1\n",
- "200001 keith02 1 1 1 1 1"
- ]
- },
- "execution_count": 21,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015.loc[200000] = ('keith01', 1, 1, 1, 1, 1)\n",
- "df_min_2015.loc[200001] = ['keith02', 1, 1, 1, 1, 1]\n",
- "\n",
- "df_min_2015.tail()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Note that we can drop a number of rows at a time by passing a list of the indices we'd like dropped."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>101189</th>\n",
- " <td>thielca01</td>\n",
- " <td>6</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100917</th>\n",
- " <td>polanjo01</td>\n",
- " <td>4</td>\n",
- " <td>10</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99954</th>\n",
- " <td>bernido01</td>\n",
- " <td>4</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>2</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100564</th>\n",
- " <td>keplema01</td>\n",
- " <td>3</td>\n",
- " <td>7</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100729</th>\n",
- " <td>meyeral01</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI\n",
- "101189 thielca01 6 0 0 0 0\n",
- "100917 polanjo01 4 10 3 0 1\n",
- "99954 bernido01 4 5 1 0 2\n",
- "100564 keplema01 3 7 1 0 0\n",
- "100729 meyeral01 2 0 0 0 0"
- ]
- },
- "execution_count": 22,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015 = df_min_2015.drop([200000, 200001], axis=0)\n",
- "df_min_2015.tail()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>100917</th>\n",
- " <td>polanjo01</td>\n",
- " <td>4</td>\n",
- " <td>10</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99954</th>\n",
- " <td>bernido01</td>\n",
- " <td>4</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>2</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100564</th>\n",
- " <td>keplema01</td>\n",
- " <td>3</td>\n",
- " <td>7</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100729</th>\n",
- " <td>meyeral01</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>200000</th>\n",
- " <td>keith01</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI\n",
- "100917 polanjo01 4 10 3 0 1\n",
- "99954 bernido01 4 5 1 0 2\n",
- "100564 keplema01 3 7 1 0 0\n",
- "100729 meyeral01 2 0 0 0 0\n",
- "200000 keith01 0 0 0 0 0"
- ]
- },
- "execution_count": 23,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015.append(\n",
- " pd.Series( \n",
- " {'playerID': 'keith01', \n",
- " 'G': 0, \n",
- " 'AB': 0, \n",
- " 'H':0, \n",
- " 'HR': 0, \n",
- " 'RBI': 0}, name='200000')).tail()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>158</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>152</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>139</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>131</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101189</th>\n",
- " <td>thielca01</td>\n",
- " <td>6</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100917</th>\n",
- " <td>polanjo01</td>\n",
- " <td>4</td>\n",
- " <td>10</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99954</th>\n",
- " <td>bernido01</td>\n",
- " <td>4</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>2</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100564</th>\n",
- " <td>keplema01</td>\n",
- " <td>3</td>\n",
- " <td>7</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100729</th>\n",
- " <td>meyeral01</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI\n",
- "100696 mauerjo01 158 592 157 10 66\n",
- "100215 doziebr01 157 628 148 28 77\n",
- "100915 plouftr01 152 573 140 22 86\n",
- "100488 hunteto01 139 521 125 22 81\n",
- "101164 suzukku01 131 433 104 5 50\n",
- "101189 thielca01 6 0 0 0 0\n",
- "100917 polanjo01 4 10 3 0 1\n",
- "99954 bernido01 4 5 1 0 2\n",
- "100564 keplema01 3 7 1 0 0\n",
- "100729 meyeral01 2 0 0 0 0"
- ]
- },
- "execution_count": 24,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015[:5].append(df_min_2015[-5:])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>158</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>152</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>139</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>131</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101067</th>\n",
- " <td>sanomi01</td>\n",
- " <td>80</td>\n",
- " <td>279</td>\n",
- " <td>75</td>\n",
- " <td>18</td>\n",
- " <td>52</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100816</th>\n",
- " <td>nunezed02</td>\n",
- " <td>72</td>\n",
- " <td>188</td>\n",
- " <td>53</td>\n",
- " <td>4</td>\n",
- " <td>20</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101189</th>\n",
- " <td>thielca01</td>\n",
- " <td>6</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100917</th>\n",
- " <td>polanjo01</td>\n",
- " <td>4</td>\n",
- " <td>10</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99954</th>\n",
- " <td>bernido01</td>\n",
- " <td>4</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>2</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100564</th>\n",
- " <td>keplema01</td>\n",
- " <td>3</td>\n",
- " <td>7</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100729</th>\n",
- " <td>meyeral01</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI\n",
- "100696 mauerjo01 158 592 157 10 66\n",
- "100215 doziebr01 157 628 148 28 77\n",
- "100915 plouftr01 152 573 140 22 86\n",
- "100488 hunteto01 139 521 125 22 81\n",
- "101164 suzukku01 131 433 104 5 50\n",
- "101067 sanomi01 80 279 75 18 52\n",
- "100816 nunezed02 72 188 53 4 20\n",
- "101189 thielca01 6 0 0 0 0\n",
- "100917 polanjo01 4 10 3 0 1\n",
- "99954 bernido01 4 5 1 0 2\n",
- "100564 keplema01 3 7 1 0 0\n",
- "100729 meyeral01 2 0 0 0 0"
- ]
- },
- "execution_count": 25,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_min_2015[:5].append([df_min_2015[10:12], df_min_2015[-5:]])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>158</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>152</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>139</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>131</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101189</th>\n",
- " <td>thielca01</td>\n",
- " <td>6</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100917</th>\n",
- " <td>polanjo01</td>\n",
- " <td>4</td>\n",
- " <td>10</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>99954</th>\n",
- " <td>bernido01</td>\n",
- " <td>4</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>2</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100564</th>\n",
- " <td>keplema01</td>\n",
- " <td>3</td>\n",
- " <td>7</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100729</th>\n",
- " <td>meyeral01</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI\n",
- "100696 mauerjo01 158 592 157 10 66\n",
- "100215 doziebr01 157 628 148 28 77\n",
- "100915 plouftr01 152 573 140 22 86\n",
- "100488 hunteto01 139 521 125 22 81\n",
- "101164 suzukku01 131 433 104 5 50\n",
- "101189 thielca01 6 0 0 0 0\n",
- "100917 polanjo01 4 10 3 0 1\n",
- "99954 bernido01 4 5 1 0 2\n",
- "100564 keplema01 3 7 1 0 0\n",
- "100729 meyeral01 2 0 0 0 0"
- ]
- },
- "execution_count": 26,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "pd.concat([df_min_2015[:5], \n",
- " df_min_2015[-5:]], axis=0)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "But we can use `concat()` to make a _column-wise_ concatenation using `axis=1` (columns). "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " <th>playerID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>99954</th>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>bernido01</td>\n",
- " <td>4</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>2</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>139</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100564</th>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>keplema01</td>\n",
- " <td>3</td>\n",
- " <td>7</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>158</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100729</th>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>meyeral01</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>152</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100917</th>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>polanjo01</td>\n",
- " <td>4</td>\n",
- " <td>10</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>131</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101189</th>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>thielca01</td>\n",
- " <td>6</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID G AB H HR RBI playerID G AB H HR RBI\n",
- "99954 NaN NaN NaN NaN NaN NaN bernido01 4 5 1 0 2\n",
- "100215 doziebr01 157 628 148 28 77 NaN NaN NaN NaN NaN NaN\n",
- "100488 hunteto01 139 521 125 22 81 NaN NaN NaN NaN NaN NaN\n",
- "100564 NaN NaN NaN NaN NaN NaN keplema01 3 7 1 0 0\n",
- "100696 mauerjo01 158 592 157 10 66 NaN NaN NaN NaN NaN NaN\n",
- "100729 NaN NaN NaN NaN NaN NaN meyeral01 2 0 0 0 0\n",
- "100915 plouftr01 152 573 140 22 86 NaN NaN NaN NaN NaN NaN\n",
- "100917 NaN NaN NaN NaN NaN NaN polanjo01 4 10 3 0 1\n",
- "101164 suzukku01 131 433 104 5 50 NaN NaN NaN NaN NaN NaN\n",
- "101189 NaN NaN NaN NaN NaN NaN thielca01 6 0 0 0 0"
- ]
- },
- "execution_count": 27,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "pd.concat([df_min_2015[:5], \n",
- " df_min_2015[-5:]], axis=1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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",
- "\n",
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>0</th>\n",
- " <th>1</th>\n",
- " <th>2</th>\n",
- " <th>3</th>\n",
- " <th>4</th>\n",
- " <th>5</th>\n",
- " <th>6</th>\n",
- " <th>7</th>\n",
- " <th>8</th>\n",
- " <th>9</th>\n",
- " <th>10</th>\n",
- " <th>11</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>99954</th>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>bernido01</td>\n",
- " <td>4</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>2</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100215</th>\n",
- " <td>doziebr01</td>\n",
- " <td>157</td>\n",
- " <td>628</td>\n",
- " <td>148</td>\n",
- " <td>28</td>\n",
- " <td>77</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100488</th>\n",
- " <td>hunteto01</td>\n",
- " <td>139</td>\n",
- " <td>521</td>\n",
- " <td>125</td>\n",
- " <td>22</td>\n",
- " <td>81</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100564</th>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>keplema01</td>\n",
- " <td>3</td>\n",
- " <td>7</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100696</th>\n",
- " <td>mauerjo01</td>\n",
- " <td>158</td>\n",
- " <td>592</td>\n",
- " <td>157</td>\n",
- " <td>10</td>\n",
- " <td>66</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100729</th>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>meyeral01</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100915</th>\n",
- " <td>plouftr01</td>\n",
- " <td>152</td>\n",
- " <td>573</td>\n",
- " <td>140</td>\n",
- " <td>22</td>\n",
- " <td>86</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100917</th>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>polanjo01</td>\n",
- " <td>4</td>\n",
- " <td>10</td>\n",
- " <td>3</td>\n",
- " <td>0</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101164</th>\n",
- " <td>suzukku01</td>\n",
- " <td>131</td>\n",
- " <td>433</td>\n",
- " <td>104</td>\n",
- " <td>5</td>\n",
- " <td>50</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>101189</th>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>thielca01</td>\n",
- " <td>6</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " 0 1 2 3 4 5 6 7 8 9 10 11\n",
- "99954 NaN NaN NaN NaN NaN NaN bernido01 4 5 1 0 2\n",
- "100215 doziebr01 157 628 148 28 77 NaN NaN NaN NaN NaN NaN\n",
- "100488 hunteto01 139 521 125 22 81 NaN NaN NaN NaN NaN NaN\n",
- "100564 NaN NaN NaN NaN NaN NaN keplema01 3 7 1 0 0\n",
- "100696 mauerjo01 158 592 157 10 66 NaN NaN NaN NaN NaN NaN\n",
- "100729 NaN NaN NaN NaN NaN NaN meyeral01 2 0 0 0 0\n",
- "100915 plouftr01 152 573 140 22 86 NaN NaN NaN NaN NaN NaN\n",
- "100917 NaN NaN NaN NaN NaN NaN polanjo01 4 10 3 0 1\n",
- "101164 suzukku01 131 433 104 5 50 NaN NaN NaN NaN NaN NaN\n",
- "101189 NaN NaN NaN NaN NaN NaN thielca01 6 0 0 0 0"
- ]
- },
- "execution_count": 28,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "pd.concat([df_min_2015[:5], \n",
- " df_min_2015[-5:]], axis=1, ignore_index=True)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Advanced indexing\n",
- "Pandas provides the ability to build more complex indices allowing for highly flexible and natural data access.\n",
- "\n",
- "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."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let's get the players on the Washington Nationals who played 100 or more games in 2015 and 2016."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "df_was = df[(df.yearID > 2014) & (df.teamID=='WAS') & (df.G > 99)]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>yearID</th>\n",
- " <th>stint</th>\n",
- " <th>teamID</th>\n",
- " <th>lgID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>R</th>\n",
- " <th>H</th>\n",
- " <th>2B</th>\n",
- " <th>...</th>\n",
- " <th>RBI</th>\n",
- " <th>SB</th>\n",
- " <th>CS</th>\n",
- " <th>BB</th>\n",
- " <th>SO</th>\n",
- " <th>IBB</th>\n",
- " <th>HBP</th>\n",
- " <th>SH</th>\n",
- " <th>SF</th>\n",
- " <th>GIDP</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>100193</th>\n",
- " <td>desmoia01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>WAS</td>\n",
- " <td>NL</td>\n",
- " <td>156</td>\n",
- " <td>583</td>\n",
- " <td>69</td>\n",
- " <td>136</td>\n",
- " <td>27</td>\n",
- " <td>...</td>\n",
- " <td>62.0</td>\n",
- " <td>13.0</td>\n",
- " <td>5.0</td>\n",
- " <td>45</td>\n",
- " <td>187.0</td>\n",
- " <td>0.0</td>\n",
- " <td>3.0</td>\n",
- " <td>6.0</td>\n",
- " <td>4.0</td>\n",
- " <td>9.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100250</th>\n",
- " <td>escobyu01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>WAS</td>\n",
- " <td>NL</td>\n",
- " <td>139</td>\n",
- " <td>535</td>\n",
- " <td>75</td>\n",
- " <td>168</td>\n",
- " <td>25</td>\n",
- " <td>...</td>\n",
- " <td>56.0</td>\n",
- " <td>2.0</td>\n",
- " <td>2.0</td>\n",
- " <td>45</td>\n",
- " <td>70.0</td>\n",
- " <td>0.0</td>\n",
- " <td>8.0</td>\n",
- " <td>1.0</td>\n",
- " <td>2.0</td>\n",
- " <td>24.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100251</th>\n",
- " <td>espinda01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>WAS</td>\n",
- " <td>NL</td>\n",
- " <td>118</td>\n",
- " <td>367</td>\n",
- " <td>59</td>\n",
- " <td>88</td>\n",
- " <td>21</td>\n",
- " <td>...</td>\n",
- " <td>37.0</td>\n",
- " <td>5.0</td>\n",
- " <td>2.0</td>\n",
- " <td>33</td>\n",
- " <td>106.0</td>\n",
- " <td>5.0</td>\n",
- " <td>6.0</td>\n",
- " <td>3.0</td>\n",
- " <td>3.0</td>\n",
- " <td>6.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100422</th>\n",
- " <td>harpebr03</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>WAS</td>\n",
- " <td>NL</td>\n",
- " <td>153</td>\n",
- " <td>521</td>\n",
- " <td>118</td>\n",
- " <td>172</td>\n",
- " <td>38</td>\n",
- " <td>...</td>\n",
- " <td>99.0</td>\n",
- " <td>6.0</td>\n",
- " <td>4.0</td>\n",
- " <td>124</td>\n",
- " <td>131.0</td>\n",
- " <td>15.0</td>\n",
- " <td>5.0</td>\n",
- " <td>0.0</td>\n",
- " <td>4.0</td>\n",
- " <td>15.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>100950</th>\n",
- " <td>ramoswi01</td>\n",
- " <td>2015</td>\n",
- " <td>1</td>\n",
- " <td>WAS</td>\n",
- " <td>NL</td>\n",
- " <td>128</td>\n",
- " <td>475</td>\n",
- " <td>41</td>\n",
- " <td>109</td>\n",
- " <td>16</td>\n",
- " <td>...</td>\n",
- " <td>68.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>21</td>\n",
- " <td>101.0</td>\n",
- " <td>2.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>8.0</td>\n",
- " <td>16.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "<p>5 rows × 22 columns</p>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID yearID stint teamID lgID G AB R H 2B ... \\\n",
- "100193 desmoia01 2015 1 WAS NL 156 583 69 136 27 ... \n",
- "100250 escobyu01 2015 1 WAS NL 139 535 75 168 25 ... \n",
- "100251 espinda01 2015 1 WAS NL 118 367 59 88 21 ... \n",
- "100422 harpebr03 2015 1 WAS NL 153 521 118 172 38 ... \n",
- "100950 ramoswi01 2015 1 WAS NL 128 475 41 109 16 ... \n",
- "\n",
- " RBI SB CS BB SO IBB HBP SH SF GIDP \n",
- "100193 62.0 13.0 5.0 45 187.0 0.0 3.0 6.0 4.0 9.0 \n",
- "100250 56.0 2.0 2.0 45 70.0 0.0 8.0 1.0 2.0 24.0 \n",
- "100251 37.0 5.0 2.0 33 106.0 5.0 6.0 3.0 3.0 6.0 \n",
- "100422 99.0 6.0 4.0 124 131.0 15.0 5.0 0.0 4.0 15.0 \n",
- "100950 68.0 0.0 0.0 21 101.0 2.0 0.0 0.0 8.0 16.0 \n",
- "\n",
- "[5 rows x 22 columns]"
- ]
- },
- "execution_count": 30,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_was.head()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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",
- "\n",
- "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",
- "\n",
- "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:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "(('desmoia01', 2015),\n",
- " ('escobyu01', 2015),\n",
- " ('espinda01', 2015),\n",
- " ('espinda01', 2016),\n",
- " ('harpebr03', 2015),\n",
- " ('harpebr03', 2016),\n",
- " ('murphda08', 2016),\n",
- " ('ramoswi01', 2015),\n",
- " ('ramoswi01', 2016),\n",
- " ('rendoan01', 2016),\n",
- " ('reverbe01', 2016),\n",
- " ('robincl01', 2015),\n",
- " ('robincl01', 2016),\n",
- " ('taylomi02', 2015),\n",
- " ('werthja01', 2016),\n",
- " ('zimmery01', 2016))"
- ]
- },
- "execution_count": 31,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "tuple(\n",
- "zip(\n",
- " df_was[['playerID','yearID']].sort_values(by='playerID')['playerID'],\n",
- " df_was[['playerID','yearID']].sort_values(by='playerID')['yearID']\n",
- ")\n",
- ")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "MultiIndex(levels=[['desmoia01', 'escobyu01', 'espinda01', 'harpebr03', 'murphda08', 'ramoswi01', 'rendoan01', 'reverbe01', 'robincl01', 'taylomi02', 'werthja01', 'zimmery01'], [2015, 2016]],\n",
- " 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]])"
- ]
- },
- "execution_count": 32,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# create an index to be used over the data we're interested in\n",
- "idx = \\\n",
- " pd.MultiIndex.from_tuples(\n",
- " tuple(\n",
- " zip(\n",
- " df_was[['playerID','yearID']].sort_values(by='playerID')['playerID'],\n",
- " df_was[['playerID','yearID']].sort_values(by='playerID')['yearID']))\n",
- " )\n",
- "idx"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>R</th>\n",
- " <th>H</th>\n",
- " <th>2B</th>\n",
- " <th>3B</th>\n",
- " <th>HR</th>\n",
- " <th>RBI</th>\n",
- " <th>SB</th>\n",
- " <th>CS</th>\n",
- " <th>BB</th>\n",
- " <th>SO</th>\n",
- " <th>IBB</th>\n",
- " <th>HBP</th>\n",
- " <th>SH</th>\n",
- " <th>SF</th>\n",
- " <th>GIDP</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>desmoia01</th>\n",
- " <th>2015</th>\n",
- " <td>156</td>\n",
- " <td>583</td>\n",
- " <td>69</td>\n",
- " <td>136</td>\n",
- " <td>27</td>\n",
- " <td>2</td>\n",
- " <td>19</td>\n",
- " <td>62.0</td>\n",
- " <td>13.0</td>\n",
- " <td>5.0</td>\n",
- " <td>45</td>\n",
- " <td>187.0</td>\n",
- " <td>0.0</td>\n",
- " <td>3.0</td>\n",
- " <td>6.0</td>\n",
- " <td>4.0</td>\n",
- " <td>9.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>escobyu01</th>\n",
- " <th>2015</th>\n",
- " <td>139</td>\n",
- " <td>535</td>\n",
- " <td>75</td>\n",
- " <td>168</td>\n",
- " <td>25</td>\n",
- " <td>1</td>\n",
- " <td>9</td>\n",
- " <td>56.0</td>\n",
- " <td>2.0</td>\n",
- " <td>2.0</td>\n",
- " <td>45</td>\n",
- " <td>70.0</td>\n",
- " <td>0.0</td>\n",
- " <td>8.0</td>\n",
- " <td>1.0</td>\n",
- " <td>2.0</td>\n",
- " <td>24.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th rowspan=\"2\" valign=\"top\">espinda01</th>\n",
- " <th>2015</th>\n",
- " <td>118</td>\n",
- " <td>367</td>\n",
- " <td>59</td>\n",
- " <td>88</td>\n",
- " <td>21</td>\n",
- " <td>1</td>\n",
- " <td>13</td>\n",
- " <td>37.0</td>\n",
- " <td>5.0</td>\n",
- " <td>2.0</td>\n",
- " <td>33</td>\n",
- " <td>106.0</td>\n",
- " <td>5.0</td>\n",
- " <td>6.0</td>\n",
- " <td>3.0</td>\n",
- " <td>3.0</td>\n",
- " <td>6.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2016</th>\n",
- " <td>157</td>\n",
- " <td>516</td>\n",
- " <td>66</td>\n",
- " <td>108</td>\n",
- " <td>15</td>\n",
- " <td>0</td>\n",
- " <td>24</td>\n",
- " <td>72.0</td>\n",
- " <td>9.0</td>\n",
- " <td>2.0</td>\n",
- " <td>54</td>\n",
- " <td>174.0</td>\n",
- " <td>12.0</td>\n",
- " <td>20.0</td>\n",
- " <td>7.0</td>\n",
- " <td>4.0</td>\n",
- " <td>4.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th rowspan=\"2\" valign=\"top\">harpebr03</th>\n",
- " <th>2015</th>\n",
- " <td>153</td>\n",
- " <td>521</td>\n",
- " <td>118</td>\n",
- " <td>172</td>\n",
- " <td>38</td>\n",
- " <td>1</td>\n",
- " <td>42</td>\n",
- " <td>99.0</td>\n",
- " <td>6.0</td>\n",
- " <td>4.0</td>\n",
- " <td>124</td>\n",
- " <td>131.0</td>\n",
- " <td>15.0</td>\n",
- " <td>5.0</td>\n",
- " <td>0.0</td>\n",
- " <td>4.0</td>\n",
- " <td>15.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2016</th>\n",
- " <td>147</td>\n",
- " <td>506</td>\n",
- " <td>84</td>\n",
- " <td>123</td>\n",
- " <td>24</td>\n",
- " <td>2</td>\n",
- " <td>24</td>\n",
- " <td>86.0</td>\n",
- " <td>21.0</td>\n",
- " <td>10.0</td>\n",
- " <td>108</td>\n",
- " <td>117.0</td>\n",
- " <td>20.0</td>\n",
- " <td>3.0</td>\n",
- " <td>0.0</td>\n",
- " <td>10.0</td>\n",
- " <td>11.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>murphda08</th>\n",
- " <th>2016</th>\n",
- " <td>142</td>\n",
- " <td>531</td>\n",
- " <td>88</td>\n",
- " <td>184</td>\n",
- " <td>47</td>\n",
- " <td>5</td>\n",
- " <td>25</td>\n",
- " <td>104.0</td>\n",
- " <td>5.0</td>\n",
- " <td>3.0</td>\n",
- " <td>35</td>\n",
- " <td>57.0</td>\n",
- " <td>10.0</td>\n",
- " <td>8.0</td>\n",
- " <td>0.0</td>\n",
- " <td>8.0</td>\n",
- " <td>4.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th rowspan=\"2\" valign=\"top\">ramoswi01</th>\n",
- " <th>2015</th>\n",
- " <td>128</td>\n",
- " <td>475</td>\n",
- " <td>41</td>\n",
- " <td>109</td>\n",
- " <td>16</td>\n",
- " <td>0</td>\n",
- " <td>15</td>\n",
- " <td>68.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>21</td>\n",
- " <td>101.0</td>\n",
- " <td>2.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>8.0</td>\n",
- " <td>16.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2016</th>\n",
- " <td>131</td>\n",
- " <td>482</td>\n",
- " <td>58</td>\n",
- " <td>148</td>\n",
- " <td>25</td>\n",
- " <td>0</td>\n",
- " <td>22</td>\n",
- " <td>80.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>35</td>\n",
- " <td>79.0</td>\n",
- " <td>2.0</td>\n",
- " <td>2.0</td>\n",
- " <td>0.0</td>\n",
- " <td>4.0</td>\n",
- " <td>17.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>rendoan01</th>\n",
- " <th>2016</th>\n",
- " <td>156</td>\n",
- " <td>567</td>\n",
- " <td>91</td>\n",
- " <td>153</td>\n",
- " <td>38</td>\n",
- " <td>2</td>\n",
- " <td>20</td>\n",
- " <td>85.0</td>\n",
- " <td>12.0</td>\n",
- " <td>6.0</td>\n",
- " <td>65</td>\n",
- " <td>117.0</td>\n",
- " <td>2.0</td>\n",
- " <td>7.0</td>\n",
- " <td>0.0</td>\n",
- " <td>8.0</td>\n",
- " <td>5.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>reverbe01</th>\n",
- " <th>2016</th>\n",
- " <td>103</td>\n",
- " <td>350</td>\n",
- " <td>44</td>\n",
- " <td>76</td>\n",
- " <td>9</td>\n",
- " <td>7</td>\n",
- " <td>2</td>\n",
- " <td>24.0</td>\n",
- " <td>14.0</td>\n",
- " <td>5.0</td>\n",
- " <td>18</td>\n",
- " <td>34.0</td>\n",
- " <td>0.0</td>\n",
- " <td>3.0</td>\n",
- " <td>2.0</td>\n",
- " <td>2.0</td>\n",
- " <td>12.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th rowspan=\"2\" valign=\"top\">robincl01</th>\n",
- " <th>2015</th>\n",
- " <td>126</td>\n",
- " <td>309</td>\n",
- " <td>44</td>\n",
- " <td>84</td>\n",
- " <td>15</td>\n",
- " <td>1</td>\n",
- " <td>10</td>\n",
- " <td>34.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>37</td>\n",
- " <td>52.0</td>\n",
- " <td>4.0</td>\n",
- " <td>5.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>6.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2016</th>\n",
- " <td>104</td>\n",
- " <td>196</td>\n",
- " <td>16</td>\n",
- " <td>46</td>\n",
- " <td>4</td>\n",
- " <td>0</td>\n",
- " <td>5</td>\n",
- " <td>26.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>20</td>\n",
- " <td>38.0</td>\n",
- " <td>0.0</td>\n",
- " <td>2.0</td>\n",
- " <td>1.0</td>\n",
- " <td>5.0</td>\n",
- " <td>4.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>taylomi02</th>\n",
- " <th>2015</th>\n",
- " <td>138</td>\n",
- " <td>472</td>\n",
- " <td>49</td>\n",
- " <td>108</td>\n",
- " <td>15</td>\n",
- " <td>2</td>\n",
- " <td>14</td>\n",
- " <td>63.0</td>\n",
- " <td>16.0</td>\n",
- " <td>3.0</td>\n",
- " <td>35</td>\n",
- " <td>158.0</td>\n",
- " <td>9.0</td>\n",
- " <td>1.0</td>\n",
- " <td>1.0</td>\n",
- " <td>2.0</td>\n",
- " <td>5.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>werthja01</th>\n",
- " <th>2016</th>\n",
- " <td>143</td>\n",
- " <td>525</td>\n",
- " <td>84</td>\n",
- " <td>128</td>\n",
- " <td>28</td>\n",
- " <td>0</td>\n",
- " <td>21</td>\n",
- " <td>69.0</td>\n",
- " <td>5.0</td>\n",
- " <td>1.0</td>\n",
- " <td>71</td>\n",
- " <td>139.0</td>\n",
- " <td>0.0</td>\n",
- " <td>4.0</td>\n",
- " <td>0.0</td>\n",
- " <td>6.0</td>\n",
- " <td>17.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>zimmery01</th>\n",
- " <th>2016</th>\n",
- " <td>115</td>\n",
- " <td>427</td>\n",
- " <td>60</td>\n",
- " <td>93</td>\n",
- " <td>18</td>\n",
- " <td>1</td>\n",
- " <td>15</td>\n",
- " <td>46.0</td>\n",
- " <td>4.0</td>\n",
- " <td>1.0</td>\n",
- " <td>29</td>\n",
- " <td>104.0</td>\n",
- " <td>1.0</td>\n",
- " <td>5.0</td>\n",
- " <td>0.0</td>\n",
- " <td>6.0</td>\n",
- " <td>12.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " G AB R H 2B 3B HR RBI SB CS BB SO \\\n",
- "desmoia01 2015 156 583 69 136 27 2 19 62.0 13.0 5.0 45 187.0 \n",
- "escobyu01 2015 139 535 75 168 25 1 9 56.0 2.0 2.0 45 70.0 \n",
- "espinda01 2015 118 367 59 88 21 1 13 37.0 5.0 2.0 33 106.0 \n",
- " 2016 157 516 66 108 15 0 24 72.0 9.0 2.0 54 174.0 \n",
- "harpebr03 2015 153 521 118 172 38 1 42 99.0 6.0 4.0 124 131.0 \n",
- " 2016 147 506 84 123 24 2 24 86.0 21.0 10.0 108 117.0 \n",
- "murphda08 2016 142 531 88 184 47 5 25 104.0 5.0 3.0 35 57.0 \n",
- "ramoswi01 2015 128 475 41 109 16 0 15 68.0 0.0 0.0 21 101.0 \n",
- " 2016 131 482 58 148 25 0 22 80.0 0.0 0.0 35 79.0 \n",
- "rendoan01 2016 156 567 91 153 38 2 20 85.0 12.0 6.0 65 117.0 \n",
- "reverbe01 2016 103 350 44 76 9 7 2 24.0 14.0 5.0 18 34.0 \n",
- "robincl01 2015 126 309 44 84 15 1 10 34.0 0.0 0.0 37 52.0 \n",
- " 2016 104 196 16 46 4 0 5 26.0 0.0 0.0 20 38.0 \n",
- "taylomi02 2015 138 472 49 108 15 2 14 63.0 16.0 3.0 35 158.0 \n",
- "werthja01 2016 143 525 84 128 28 0 21 69.0 5.0 1.0 71 139.0 \n",
- "zimmery01 2016 115 427 60 93 18 1 15 46.0 4.0 1.0 29 104.0 \n",
- "\n",
- " IBB HBP SH SF GIDP \n",
- "desmoia01 2015 0.0 3.0 6.0 4.0 9.0 \n",
- "escobyu01 2015 0.0 8.0 1.0 2.0 24.0 \n",
- "espinda01 2015 5.0 6.0 3.0 3.0 6.0 \n",
- " 2016 12.0 20.0 7.0 4.0 4.0 \n",
- "harpebr03 2015 15.0 5.0 0.0 4.0 15.0 \n",
- " 2016 20.0 3.0 0.0 10.0 11.0 \n",
- "murphda08 2016 10.0 8.0 0.0 8.0 4.0 \n",
- "ramoswi01 2015 2.0 0.0 0.0 8.0 16.0 \n",
- " 2016 2.0 2.0 0.0 4.0 17.0 \n",
- "rendoan01 2016 2.0 7.0 0.0 8.0 5.0 \n",
- "reverbe01 2016 0.0 3.0 2.0 2.0 12.0 \n",
- "robincl01 2015 4.0 5.0 0.0 1.0 6.0 \n",
- " 2016 0.0 2.0 1.0 5.0 4.0 \n",
- "taylomi02 2015 9.0 1.0 1.0 2.0 5.0 \n",
- "werthja01 2016 0.0 4.0 0.0 6.0 17.0 \n",
- "zimmery01 2016 1.0 5.0 0.0 6.0 12.0 "
- ]
- },
- "execution_count": 33,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# sorting the indices is critical for lining up the data in the tuples\n",
- "df_was = df_was.sort_values(by=['playerID']).\\\n",
- " set_index(idx).\\\n",
- " drop(['playerID', 'yearID', 'teamID', 'lgID', 'stint'], axis=1)\n",
- "df_was"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>H</th>\n",
- " <th>SO</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>2015</th>\n",
- " <td>126</td>\n",
- " <td>309</td>\n",
- " <td>84</td>\n",
- " <td>52.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2016</th>\n",
- " <td>104</td>\n",
- " <td>196</td>\n",
- " <td>46</td>\n",
- " <td>38.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " G AB H SO\n",
- "2015 126 309 84 52.0\n",
- "2016 104 196 46 38.0"
- ]
- },
- "execution_count": 34,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_was.loc[('robincl01', ),['G', 'AB', 'H', 'SO']]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 35,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "G 104.0\n",
- "AB 196.0\n",
- "H 46.0\n",
- "SO 38.0\n",
- "Name: (robincl01, 2016), dtype: float64"
- ]
- },
- "execution_count": 35,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_was.loc[('robincl01', 2016),['G', 'AB', 'H', 'SO']]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>yearID</th>\n",
- " <th>stint</th>\n",
- " <th>teamID</th>\n",
- " <th>lgID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>R</th>\n",
- " <th>H</th>\n",
- " <th>2B</th>\n",
- " <th>...</th>\n",
- " <th>RBI</th>\n",
- " <th>SB</th>\n",
- " <th>CS</th>\n",
- " <th>BB</th>\n",
- " <th>SO</th>\n",
- " <th>IBB</th>\n",
- " <th>HBP</th>\n",
- " <th>SH</th>\n",
- " <th>SF</th>\n",
- " <th>GIDP</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>0</th>\n",
- " <td>abercda01</td>\n",
- " <td>1871</td>\n",
- " <td>1</td>\n",
- " <td>TRO</td>\n",
- " <td>NaN</td>\n",
- " <td>1</td>\n",
- " <td>4</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>1</th>\n",
- " <td>addybo01</td>\n",
- " <td>1871</td>\n",
- " <td>1</td>\n",
- " <td>RC1</td>\n",
- " <td>NaN</td>\n",
- " <td>25</td>\n",
- " <td>118</td>\n",
- " <td>30</td>\n",
- " <td>32</td>\n",
- " <td>6</td>\n",
- " <td>...</td>\n",
- " <td>13.0</td>\n",
- " <td>8.0</td>\n",
- " <td>1.0</td>\n",
- " <td>4</td>\n",
- " <td>0.0</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>2</th>\n",
- " <td>allisar01</td>\n",
- " <td>1871</td>\n",
- " <td>1</td>\n",
- " <td>CL1</td>\n",
- " <td>NaN</td>\n",
- " <td>29</td>\n",
- " <td>137</td>\n",
- " <td>28</td>\n",
- " <td>40</td>\n",
- " <td>4</td>\n",
- " <td>...</td>\n",
- " <td>19.0</td>\n",
- " <td>3.0</td>\n",
- " <td>1.0</td>\n",
- " <td>2</td>\n",
- " <td>5.0</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>3</th>\n",
- " <td>allisdo01</td>\n",
- " <td>1871</td>\n",
- " <td>1</td>\n",
- " <td>WS3</td>\n",
- " <td>NaN</td>\n",
- " <td>27</td>\n",
- " <td>133</td>\n",
- " <td>28</td>\n",
- " <td>44</td>\n",
- " <td>10</td>\n",
- " <td>...</td>\n",
- " <td>27.0</td>\n",
- " <td>1.0</td>\n",
- " <td>1.0</td>\n",
- " <td>0</td>\n",
- " <td>2.0</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>4</th>\n",
- " <td>ansonca01</td>\n",
- " <td>1871</td>\n",
- " <td>1</td>\n",
- " <td>RC1</td>\n",
- " <td>NaN</td>\n",
- " <td>25</td>\n",
- " <td>120</td>\n",
- " <td>29</td>\n",
- " <td>39</td>\n",
- " <td>11</td>\n",
- " <td>...</td>\n",
- " <td>16.0</td>\n",
- " <td>6.0</td>\n",
- " <td>2.0</td>\n",
- " <td>2</td>\n",
- " <td>1.0</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " <td>NaN</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "<p>5 rows × 22 columns</p>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID yearID stint teamID lgID G AB R H 2B ... RBI SB \\\n",
- "0 abercda01 1871 1 TRO NaN 1 4 0 0 0 ... 0.0 0.0 \n",
- "1 addybo01 1871 1 RC1 NaN 25 118 30 32 6 ... 13.0 8.0 \n",
- "2 allisar01 1871 1 CL1 NaN 29 137 28 40 4 ... 19.0 3.0 \n",
- "3 allisdo01 1871 1 WS3 NaN 27 133 28 44 10 ... 27.0 1.0 \n",
- "4 ansonca01 1871 1 RC1 NaN 25 120 29 39 11 ... 16.0 6.0 \n",
- "\n",
- " CS BB SO IBB HBP SH SF GIDP \n",
- "0 0.0 0 0.0 NaN NaN NaN NaN NaN \n",
- "1 1.0 4 0.0 NaN NaN NaN NaN NaN \n",
- "2 1.0 2 5.0 NaN NaN NaN NaN NaN \n",
- "3 1.0 0 2.0 NaN NaN NaN NaN NaN \n",
- "4 2.0 2 1.0 NaN NaN NaN NaN NaN \n",
- "\n",
- "[5 rows x 22 columns]"
- ]
- },
- "execution_count": 36,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df.head()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 37,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "((2016, 'NL', 'WAS', 'rzepcma01'),\n",
- " (2016, 'NL', 'WAS', 'scherma01'),\n",
- " (2016, 'NL', 'WAS', 'severpe01'),\n",
- " (2016, 'NL', 'WAS', 'solissa01'),\n",
- " (2016, 'NL', 'WAS', 'strasst01'),\n",
- " (2016, 'NL', 'WAS', 'taylomi02'),\n",
- " (2016, 'NL', 'WAS', 'treinbl01'),\n",
- " (2016, 'NL', 'WAS', 'turnetr01'),\n",
- " (2016, 'NL', 'WAS', 'werthja01'),\n",
- " (2016, 'NL', 'WAS', 'zimmery01'))"
- ]
- },
- "execution_count": 37,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_mi = df[df.yearID>2006].copy()\n",
- "idx_labels = ['yearID', 'lgID', 'teamID', 'playerID']\n",
- "\n",
- "tuple(\n",
- " zip(\n",
- " df_mi[idx_labels]\\\n",
- " .sort_values(idx_labels)['yearID'],\n",
- "\n",
- " df_mi[idx_labels]\\\n",
- " .sort_values(idx_labels)['lgID'],\n",
- "\n",
- " df_mi[idx_labels]\\\n",
- " .sort_values(idx_labels)['teamID'],\n",
- "\n",
- " df_mi[idx_labels]\\\n",
- " .sort_values(idx_labels)['playerID']))[-10:]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "idx = \\\n",
- " pd.MultiIndex.from_tuples(\n",
- " tuple(\n",
- " zip(\n",
- " df_mi[idx_labels]\\\n",
- " .sort_values(idx_labels)['yearID'],\n",
- " \n",
- " df_mi[idx_labels]\\\n",
- " .sort_values(idx_labels)['lgID'],\n",
- " \n",
- " df_mi[idx_labels]\\\n",
- " .sort_values(idx_labels)['teamID'],\n",
- " \n",
- " df_mi[idx_labels]\\\n",
- " .sort_values(idx_labels)['playerID']))\n",
- " )"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 39,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "df_mi = df_mi.sort_values(['yearID', 'teamID']).set_index(idx)#.drop(['playerID', 'yearID', 'teamID', 'stint'], axis=1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 40,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>yearID</th>\n",
- " <th>stint</th>\n",
- " <th>teamID</th>\n",
- " <th>lgID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>R</th>\n",
- " <th>H</th>\n",
- " <th>2B</th>\n",
- " <th>...</th>\n",
- " <th>RBI</th>\n",
- " <th>SB</th>\n",
- " <th>CS</th>\n",
- " <th>BB</th>\n",
- " <th>SO</th>\n",
- " <th>IBB</th>\n",
- " <th>HBP</th>\n",
- " <th>SH</th>\n",
- " <th>SF</th>\n",
- " <th>GIDP</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th rowspan=\"5\" valign=\"top\">2007</th>\n",
- " <th rowspan=\"5\" valign=\"top\">AL</th>\n",
- " <th rowspan=\"5\" valign=\"top\">BAL</th>\n",
- " <th>baezda01</th>\n",
- " <td>bardebr01</td>\n",
- " <td>2007</td>\n",
- " <td>1</td>\n",
- " <td>ARI</td>\n",
- " <td>NL</td>\n",
- " <td>8</td>\n",
- " <td>12</td>\n",
- " <td>0</td>\n",
- " <td>1</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>3.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>bakopa01</th>\n",
- " <td>bonifem01</td>\n",
- " <td>2007</td>\n",
- " <td>1</td>\n",
- " <td>ARI</td>\n",
- " <td>NL</td>\n",
- " <td>11</td>\n",
- " <td>23</td>\n",
- " <td>2</td>\n",
- " <td>5</td>\n",
- " <td>1</td>\n",
- " <td>...</td>\n",
- " <td>2.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>4</td>\n",
- " <td>3.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>bedarer01</th>\n",
- " <td>byrneer01</td>\n",
- " <td>2007</td>\n",
- " <td>1</td>\n",
- " <td>ARI</td>\n",
- " <td>NL</td>\n",
- " <td>160</td>\n",
- " <td>626</td>\n",
- " <td>103</td>\n",
- " <td>179</td>\n",
- " <td>30</td>\n",
- " <td>...</td>\n",
- " <td>83.0</td>\n",
- " <td>50.0</td>\n",
- " <td>7.0</td>\n",
- " <td>57</td>\n",
- " <td>98.0</td>\n",
- " <td>5.0</td>\n",
- " <td>10.0</td>\n",
- " <td>1.0</td>\n",
- " <td>4.0</td>\n",
- " <td>12.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>bellro01</th>\n",
- " <td>callaal01</td>\n",
- " <td>2007</td>\n",
- " <td>1</td>\n",
- " <td>ARI</td>\n",
- " <td>NL</td>\n",
- " <td>56</td>\n",
- " <td>144</td>\n",
- " <td>10</td>\n",
- " <td>31</td>\n",
- " <td>8</td>\n",
- " <td>...</td>\n",
- " <td>7.0</td>\n",
- " <td>1.0</td>\n",
- " <td>1.0</td>\n",
- " <td>9</td>\n",
- " <td>14.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>1.0</td>\n",
- " <td>1.0</td>\n",
- " <td>8.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>birkiku01</th>\n",
- " <td>choatra01</td>\n",
- " <td>2007</td>\n",
- " <td>1</td>\n",
- " <td>ARI</td>\n",
- " <td>NL</td>\n",
- " <td>2</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "<p>5 rows × 22 columns</p>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID yearID stint teamID lgID G AB R \\\n",
- "2007 AL BAL baezda01 bardebr01 2007 1 ARI NL 8 12 0 \n",
- " bakopa01 bonifem01 2007 1 ARI NL 11 23 2 \n",
- " bedarer01 byrneer01 2007 1 ARI NL 160 626 103 \n",
- " bellro01 callaal01 2007 1 ARI NL 56 144 10 \n",
- " birkiku01 choatra01 2007 1 ARI NL 2 0 0 \n",
- "\n",
- " H 2B ... RBI SB CS BB SO IBB HBP \\\n",
- "2007 AL BAL baezda01 1 0 ... 0.0 0.0 0.0 0 3.0 0.0 0.0 \n",
- " bakopa01 5 1 ... 2.0 0.0 1.0 4 3.0 0.0 0.0 \n",
- " bedarer01 179 30 ... 83.0 50.0 7.0 57 98.0 5.0 10.0 \n",
- " bellro01 31 8 ... 7.0 1.0 1.0 9 14.0 0.0 1.0 \n",
- " birkiku01 0 0 ... 0.0 0.0 0.0 0 0.0 0.0 0.0 \n",
- "\n",
- " SH SF GIDP \n",
- "2007 AL BAL baezda01 0.0 0.0 0.0 \n",
- " bakopa01 0.0 0.0 0.0 \n",
- " bedarer01 1.0 4.0 12.0 \n",
- " bellro01 1.0 1.0 8.0 \n",
- " birkiku01 0.0 0.0 0.0 \n",
- "\n",
- "[5 rows x 22 columns]"
- ]
- },
- "execution_count": 40,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_mi.head()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 41,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th></th>\n",
- " <th>playerID</th>\n",
- " <th>yearID</th>\n",
- " <th>stint</th>\n",
- " <th>teamID</th>\n",
- " <th>lgID</th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " <th>R</th>\n",
- " <th>H</th>\n",
- " <th>2B</th>\n",
- " <th>...</th>\n",
- " <th>RBI</th>\n",
- " <th>SB</th>\n",
- " <th>CS</th>\n",
- " <th>BB</th>\n",
- " <th>SO</th>\n",
- " <th>IBB</th>\n",
- " <th>HBP</th>\n",
- " <th>SH</th>\n",
- " <th>SF</th>\n",
- " <th>GIDP</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th rowspan=\"5\" valign=\"top\">2016</th>\n",
- " <th rowspan=\"5\" valign=\"top\">NL</th>\n",
- " <th rowspan=\"5\" valign=\"top\">WAS</th>\n",
- " <th>taylomi02</th>\n",
- " <td>taylomi02</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>WAS</td>\n",
- " <td>NL</td>\n",
- " <td>76</td>\n",
- " <td>221</td>\n",
- " <td>28</td>\n",
- " <td>51</td>\n",
- " <td>11</td>\n",
- " <td>...</td>\n",
- " <td>16.0</td>\n",
- " <td>14.0</td>\n",
- " <td>3.0</td>\n",
- " <td>14</td>\n",
- " <td>77.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>2.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>treinbl01</th>\n",
- " <td>treinbl01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>WAS</td>\n",
- " <td>NL</td>\n",
- " <td>73</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>0</td>\n",
- " <td>...</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " <td>0.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>turnetr01</th>\n",
- " <td>turnetr01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>WAS</td>\n",
- " <td>NL</td>\n",
- " <td>73</td>\n",
- " <td>307</td>\n",
- " <td>53</td>\n",
- " <td>105</td>\n",
- " <td>14</td>\n",
- " <td>...</td>\n",
- " <td>40.0</td>\n",
- " <td>33.0</td>\n",
- " <td>6.0</td>\n",
- " <td>14</td>\n",
- " <td>59.0</td>\n",
- " <td>0.0</td>\n",
- " <td>1.0</td>\n",
- " <td>0.0</td>\n",
- " <td>2.0</td>\n",
- " <td>1.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>werthja01</th>\n",
- " <td>werthja01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>WAS</td>\n",
- " <td>NL</td>\n",
- " <td>143</td>\n",
- " <td>525</td>\n",
- " <td>84</td>\n",
- " <td>128</td>\n",
- " <td>28</td>\n",
- " <td>...</td>\n",
- " <td>69.0</td>\n",
- " <td>5.0</td>\n",
- " <td>1.0</td>\n",
- " <td>71</td>\n",
- " <td>139.0</td>\n",
- " <td>0.0</td>\n",
- " <td>4.0</td>\n",
- " <td>0.0</td>\n",
- " <td>6.0</td>\n",
- " <td>17.0</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>zimmery01</th>\n",
- " <td>zimmery01</td>\n",
- " <td>2016</td>\n",
- " <td>1</td>\n",
- " <td>WAS</td>\n",
- " <td>NL</td>\n",
- " <td>115</td>\n",
- " <td>427</td>\n",
- " <td>60</td>\n",
- " <td>93</td>\n",
- " <td>18</td>\n",
- " <td>...</td>\n",
- " <td>46.0</td>\n",
- " <td>4.0</td>\n",
- " <td>1.0</td>\n",
- " <td>29</td>\n",
- " <td>104.0</td>\n",
- " <td>1.0</td>\n",
- " <td>5.0</td>\n",
- " <td>0.0</td>\n",
- " <td>6.0</td>\n",
- " <td>12.0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "<p>5 rows × 22 columns</p>\n",
- "</div>"
- ],
- "text/plain": [
- " playerID yearID stint teamID lgID G AB R \\\n",
- "2016 NL WAS taylomi02 taylomi02 2016 1 WAS NL 76 221 28 \n",
- " treinbl01 treinbl01 2016 1 WAS NL 73 0 0 \n",
- " turnetr01 turnetr01 2016 1 WAS NL 73 307 53 \n",
- " werthja01 werthja01 2016 1 WAS NL 143 525 84 \n",
- " zimmery01 zimmery01 2016 1 WAS NL 115 427 60 \n",
- "\n",
- " H 2B ... RBI SB CS BB SO IBB HBP \\\n",
- "2016 NL WAS taylomi02 51 11 ... 16.0 14.0 3.0 14 77.0 0.0 1.0 \n",
- " treinbl01 0 0 ... 0.0 0.0 0.0 0 0.0 0.0 0.0 \n",
- " turnetr01 105 14 ... 40.0 33.0 6.0 14 59.0 0.0 1.0 \n",
- " werthja01 128 28 ... 69.0 5.0 1.0 71 139.0 0.0 4.0 \n",
- " zimmery01 93 18 ... 46.0 4.0 1.0 29 104.0 1.0 5.0 \n",
- "\n",
- " SH SF GIDP \n",
- "2016 NL WAS taylomi02 0.0 1.0 2.0 \n",
- " treinbl01 0.0 0.0 0.0 \n",
- " turnetr01 0.0 2.0 1.0 \n",
- " werthja01 0.0 6.0 17.0 \n",
- " zimmery01 0.0 6.0 12.0 \n",
- "\n",
- "[5 rows x 22 columns]"
- ]
- },
- "execution_count": 41,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_mi.tail()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "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."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/html": [
- "<div>\n",
- "<style>\n",
- " .dataframe thead tr:only-child th {\n",
- " text-align: right;\n",
- " }\n",
- "\n",
- " .dataframe thead th {\n",
- " text-align: left;\n",
- " }\n",
- "\n",
- " .dataframe tbody tr th {\n",
- " vertical-align: top;\n",
- " }\n",
- "</style>\n",
- "<table border=\"1\" class=\"dataframe\">\n",
- " <thead>\n",
- " <tr style=\"text-align: right;\">\n",
- " <th></th>\n",
- " <th>G</th>\n",
- " <th>AB</th>\n",
- " </tr>\n",
- " </thead>\n",
- " <tbody>\n",
- " <tr>\n",
- " <th>accarje01</th>\n",
- " <td>152</td>\n",
- " <td>509</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>adamsru01</th>\n",
- " <td>62</td>\n",
- " <td>1</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>banksjo01</th>\n",
- " <td>26</td>\n",
- " <td>5</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>burneaj01</th>\n",
- " <td>8</td>\n",
- " <td>14</td>\n",
- " </tr>\n",
- " <tr>\n",
- " <th>chacigu01</th>\n",
- " <td>65</td>\n",
- " <td>0</td>\n",
- " </tr>\n",
- " </tbody>\n",
- "</table>\n",
- "</div>"
- ],
- "text/plain": [
- " G AB\n",
- "accarje01 152 509\n",
- "adamsru01 62 1\n",
- "banksjo01 26 5\n",
- "burneaj01 8 14\n",
- "chacigu01 65 0"
- ]
- },
- "execution_count": 42,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "df_mi.loc[(2007, 'AL', 'TOR'), ['G', 'AB']].head()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Ξ"
- ]
- }
- ],
- "metadata": {
- "anaconda-cloud": {},
- "gist": {
- "data": {
- "description": "nb/2_dataframe_operations.ipynb",
- "public": false
- },
- "id": ""
- },
- "kernelspec": {
- "display_name": "Python [default]",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.1"
- },
- "toc": {
- "colors": {
- "hover_highlight": "#DAA520",
- "navigate_num": "#000000",
- "navigate_text": "#333333",
- "running_highlight": "#FF0000",
- "selected_highlight": "#FFD700",
- "sidebar_border": "#EEEEEE",
- "wrapper_background": "#FFFFFF"
- },
- "moveMenuLeft": true,
- "nav_menu": {
- "height": "211px",
- "width": "252px"
- },
- "navigate_menu": true,
- "number_sections": false,
- "sideBar": true,
- "threshold": 4,
- "toc_cell": true,
- "toc_section_display": "block",
- "toc_window_display": true,
- "widenNotebook": false
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
|