Sunteți pe pagina 1din 28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

IntrotoDataStructures
Wellstartwithaquick,noncomprehensiveoverviewofthefundamentaldatastructuresinpandas togetyoustarted.Thefundamentalbehavioraboutdatatypes,indexing,andaxislabeling/ alignmentapplyacrossalloftheobjects.Togetstarted,importnumpyandloadpandasintoyour namespace:

I n[ 1 ] :i m p o r tn u m p ya sn p #w i l lu s eal o ti ne x a m p l e s I n[ 2 ] :r a n d n=n p . r a n d o m . r a n d n I n[ 3 ] :f r o mp a n d a si m p o r t*

Hereisabasictenettokeepinmind:dataalignmentisintrinsic.Thelinkbetweenlabelsanddata willnotbebrokenunlessdonesoexplicitlybyyou. Wellgiveabriefintrotothedatastructures,thenconsiderallofthebroadcategoriesoffunctionality andmethodsinseparatesections. Whenusingpandas,werecommendthefollowingimportconvention:

i m p o r tp a n d a sa sp d

Series
S e r i e s isaonedimensionallabeledarray(technicallyasubclassofndarray)capableofholding

anydatatype(integers,strings,floatingpointnumbers,Pythonobjects,etc.).Theaxislabelsare collectivelyreferredtoastheindex.ThebasicmethodtocreateaSeriesistocall:

> > >s=S e r i e s ( d a t a ,i n d e x = i n d e x )

Here,d a t a canbemanydifferentthings: aPythondict anndarray ascalarvalue(like5) Thepassedindexisalistofaxislabels.Thus,thisseparatesintoafewcasesdependingonwhat datais: Fromndarray


pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 1/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

Ifd a t a isanndarray,indexmustbethesamelengthasdata.Ifnoindexispassed,onewillbe createdhavingvalues[ 0 ,. . . ,l e n ( d a t a )-1 ] .


I n[ 4 ] :s=S e r i e s ( r a n d n ( 5 ) ,i n d e x = [ ' a ' ,' b ' ,' c ' ,' d ' ,' e ' ] ) I n[ 5 ] :s a 1 . 3 4 4 b 0 . 8 4 5 c 1 . 0 7 6 d 0 . 1 0 9 e 1 . 6 4 4 d t y p e :f l o a t 6 4 I n[ 6 ] :s . i n d e x I n d e x ( [ u ' a ' ,u ' b ' ,u ' c ' ,u ' d ' ,u ' e ' ] ,d t y p e = o b j e c t ) I n[ 7 ] :S e r i e s ( r a n d n ( 5 ) ) 0 1 . 4 6 9 1 0 . 3 5 7 2 0 . 6 7 5 3 1 . 7 7 7 4 0 . 9 6 9 d t y p e :f l o a t 6 4

Note: Startinginv0.8.0,pandassupportsnonuniqueindexvalues.Ifanoperationthatdoesnot supportduplicateindexvaluesisattempted,anexceptionwillberaisedatthattime.Thereason forbeinglazyisnearlyallperformancebased(therearemanyinstancesincomputations,like partsofGroupBy,wheretheindexisnotused). Fromdict Ifd a t a isadict,ifindexispassedthevaluesindatacorrespondingtothelabelsintheindexwillbe pulledout.Otherwise,anindexwillbeconstructedfromthesortedkeysofthedict,ifpossible.

I n[ 8 ] :d={ ' a ':0 . ,' b ':1 . ,' c ':2 . } I n[ 9 ] :S e r i e s ( d ) a 0 b 1 c 2 d t y p e :f l o a t 6 4 I n[ 1 0 ] :S e r i e s ( d ,i n d e x = [ ' b ' ,' c ' ,' d ' ,' a ' ] ) b 1 c 2 d N a N a 0 d t y p e :f l o a t 6 4
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 2/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

Note: NaN(notanumber)isthestandardmissingdatamarkerusedinpandas FromscalarvalueIfd a t a isascalarvalue,anindexmustbeprovided.Thevaluewillberepeated tomatchthelengthofindex

I n[ 1 1 ] :S e r i e s ( 5 . ,i n d e x = [ ' a ' ,' b ' ,' c ' ,' d ' ,' e ' ] ) a 5 b 5 c 5 d 5 e 5 d t y p e :f l o a t 6 4

Seriesisndarraylike
Asasubclassofndarray,SeriesisavalidargumenttomostNumPyfunctionsandbehavessimilarly toaNumPyarray.However,thingslikeslicingalsoslicetheindex.

I n[ 1 2 ] :s [ 0 ] 1 . 3 4 4 3 1 1 8 1 2 7 3 1 6 6 7 1 I n[ 1 3 ] :s [ : 3 ] a 1 . 3 4 4 b 0 . 8 4 5 c 1 . 0 7 6 d t y p e :f l o a t 6 4 I n[ 1 4 ] :s [ s>s . m e d i a n ( ) ] c 1 . 0 7 6 e 1 . 6 4 4 d t y p e :f l o a t 6 4 I n[ 1 5 ] :s [ [ 4 ,3 ,1 ] ] e 1 . 6 4 4 d 0 . 1 0 9 b 0 . 8 4 5 d t y p e :f l o a t 6 4 I n[ 1 6 ] :n p . e x p ( s ) a 0 . 2 6 1 b 2 . 3 2 8 c 2 . 9 3 2 d 0 . 8 9 7 e 5 . 1 7 4 d t y p e :f l o a t 6 4

Wewilladdressarraybasedindexinginaseparatesection.
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 3/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

Seriesisdictlike
ASeriesislikeafixedsizedictinthatyoucangetandsetvaluesbyindexlabel:

I n[ 1 7 ] :s [ ' a ' ] 1 . 3 4 4 3 1 1 8 1 2 7 3 1 6 6 7 1 I n[ 1 8 ] :s [ ' e ' ]=1 2 . I n[ 1 9 ] :s a 1 . 3 4 4 b 0 . 8 4 5 c 1 . 0 7 6 d 0 . 1 0 9 e 1 2 . 0 0 0 d t y p e :f l o a t 6 4 I n[ 2 0 ] :' e 'i ns T r u e I n[ 2 1 ] :' f 'i ns F a l s e

Ifalabelisnotcontained,anexceptionisraised:

> > >s [ ' f ' ] K e y E r r o r :' f '

Usingtheg e t method,amissinglabelwillreturnNoneorspecifieddefault:
I n[ 2 2 ] :s . g e t ( ' f ' ) I n[ 2 3 ] :s . g e t ( ' f ' ,n p . n a n ) n a n

VectorizedoperationsandlabelalignmentwithSeries
Whendoingdataanalysis,aswithrawNumPyarraysloopingthroughSeriesvaluebyvalueis usuallynotnecessary.SeriescanbealsobepassedintomostNumPymethodsexpectingan ndarray.

I n[ 2 4 ] :s+s a b c d e 2 . 6 8 9 1 . 6 9 0 2 . 1 5 2 0 . 2 1 8 2 4 . 0 0 0
4/28

pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

d t y p e :f l o a t 6 4 I n[ 2 5 ] :s*2 a 2 . 6 8 9 b 1 . 6 9 0 c 2 . 1 5 2 d 0 . 2 1 8 e 2 4 . 0 0 0 d t y p e :f l o a t 6 4 I n[ 2 6 ] :n p . e x p ( s ) a 0 . 2 6 1 b 2 . 3 2 8 c 2 . 9 3 2 d 0 . 8 9 7 e 1 6 2 7 5 4 . 7 9 1 d t y p e :f l o a t 6 4

AkeydifferencebetweenSeriesandndarrayisthatoperationsbetweenSeriesautomaticallyalign thedatabasedonlabel.Thus,youcanwritecomputationswithoutgivingconsiderationtowhether theSeriesinvolvedhavethesamelabels.

I n[ 2 7 ] :s [ 1 : ]+s [ : 1 ] a N a N b 1 . 6 9 0 c 2 . 1 5 2 d 0 . 2 1 8 e N a N d t y p e :f l o a t 6 4

TheresultofanoperationbetweenunalignedSerieswillhavetheunion oftheindexesinvolved.Ifa labelisnotfoundinoneSeriesortheother,theresultwillbemarkedasmissing(NaN).Beingable towritecodewithoutdoinganyexplicitdataalignmentgrantsimmensefreedomandflexibilityin interactivedataanalysisandresearch.Theintegrateddataalignmentfeaturesofthepandasdata structuressetpandasapartfromthemajorityofrelatedtoolsforworkingwithlabeleddata. Note: Ingeneral,wechosetomakethedefaultresultofoperationsbetweendifferentlyindexed objectsyieldtheunion oftheindexesinordertoavoidlossofinformation.Havinganindexlabel, thoughthedataismissing,istypicallyimportantinformationaspartofacomputation.Youof coursehavetheoptionofdroppinglabelswithmissingdataviathedropnafunction.

Nameattribute
Seriescanalsohavean a m e attribute:
I n[ 2 8 ] :s=S e r i e s ( n p . r a n d o m . r a n d n ( 5 ) ,n a m e = ' s o m e t h i n g ' )
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 5/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

I n[ 2 9 ] :s 0 1 . 2 9 5 1 0 . 4 1 4 2 0 . 2 7 7 3 0 . 4 7 2 4 0 . 0 1 4 N a m e :s o m e t h i n g ,d t y p e :f l o a t 6 4 I n[ 3 0 ] :s . n a m e ' s o m e t h i n g '

TheSeriesn a m e willbeassignedautomaticallyinmanycases,inparticularwhentaking1Dslicesof DataFrameasyouwillseebelow.

DataFrame
DataFrameisa2dimensionallabeleddatastructurewithcolumnsofpotentiallydifferenttypes.You canthinkofitlikeaspreadsheetorSQLtable,oradictofSeriesobjects.Itisgenerallythemost commonlyusedpandasobject.LikeSeries,DataFrameacceptsmanydifferentkindsofinput: Dictof1Dndarrays,lists,dicts,orSeries 2Dnumpy.ndarray Structuredorrecordndarray AS e r i e s AnotherD a t a F r a m e Alongwiththedata,youcanoptionallypassindex(rowlabels)andcolumns(columnlabels) arguments.Ifyoupassanindexand/orcolumns,youareguaranteeingtheindexand/orcolumns oftheresultingDataFrame.Thus,adictofSeriesplusaspecificindexwilldiscardalldatanot matchinguptothepassedindex. Ifaxislabelsarenotpassed,theywillbeconstructedfromtheinputdatabasedoncommonsense rules.

FromdictofSeriesordicts
Theresultindexwillbetheunion oftheindexesofthevariousSeries.Ifthereareanynesteddicts, thesewillbefirstconvertedtoSeries.Ifnocolumnsarepassed,thecolumnswillbethesortedlistof dictkeys.

I n[ 3 1 ] :d={ ' o n e ':S e r i e s ( [ 1 . ,2 . ,3 . ] ,i n d e x = [ ' a ' ,' b ' ,' c ' ] ) , . . . . : ' t w o ':S e r i e s ( [ 1 . ,2 . ,3 . ,4 . ] ,i n d e x = [ ' a ' ,' b ' ,' c ' ,' d ' ] ) } . . . . : I n[ 3 2 ] :d f=D a t a F r a m e ( d )
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 6/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

I n[ 3 3 ] :d f o n e t w o a 1 1 b 2 2 c 3 3 d N a N 4 I n[ 3 4 ] :D a t a F r a m e ( d ,i n d e x = [ ' d ' ,' b ' ,' a ' ] ) o n e t w o d N a N 4 b 2 2 a 1 1 I n[ 3 5 ] :D a t a F r a m e ( d ,i n d e x = [ ' d ' ,' b ' ,' a ' ] ,c o l u m n s = [ ' t w o ' ,' t h r e e ' ] ) d b a t w ot h r e e 4 N a N 2 N a N 1 N a N

Therowandcolumnlabelscanbeaccessedrespectivelybyaccessingtheindexandcolumns attributes: Note: Whenaparticularsetofcolumnsispassedalongwithadictofdata,thepassedcolumns overridethekeysinthedict.

I n[ 3 6 ] :d f . i n d e x I n d e x ( [ u ' a ' ,u ' b ' ,u ' c ' ,u ' d ' ] ,d t y p e = o b j e c t ) I n[ 3 7 ] :d f . c o l u m n s I n d e x ( [ u ' o n e ' ,u ' t w o ' ] ,d t y p e = o b j e c t )

Fromdictofndarrays/lists
Thendarraysmustallbethesamelength.Ifanindexispassed,itmustclearlyalsobethesame lengthasthearrays.Ifnoindexispassed,theresultwillber a n g e ( n ) ,wheren isthearraylength.

I n[ 3 8 ] :d={ ' o n e ':[ 1 . ,2 . ,3 . ,4 . ] , . . . . : ' t w o ':[ 4 . ,3 . ,2 . ,1 . ] } . . . . : I n[ 3 9 ] :D a t a F r a m e ( d ) 0 1 2 3 o n e t w o 1 4 2 3 3 2 4 1


7/28

pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

I n[ 4 0 ] :D a t a F r a m e ( d ,i n d e x = [ ' a ' ,' b ' ,' c ' ,' d ' ] ) a b c d o n e t w o 1 4 2 3 3 2 4 1

Fromstructuredorrecordarray
Thiscaseishandledidenticallytoadictofarrays.

I n[ 4 1 ] :d a t a=n p . z e r o s ( ( 2 , ) , d t y p e = [ ( ' A ' ,' i 4 ' ) , ( ' B ' ,' f 4 ' ) , ( ' C ' ,' a 1 0 ' ) ] ) I n[ 4 2 ] :d a t a [ : ]=[ ( 1 , 2 . , ' H e l l o ' ) , ( 2 , 3 . , " W o r l d " ) ] I n[ 4 3 ] :D a t a F r a m e ( d a t a ) A B C 0 1 2 H e l l o 1 2 3 W o r l d I n[ 4 4 ] :D a t a F r a m e ( d a t a ,i n d e x = [ ' f i r s t ' ,' s e c o n d ' ] ) A B C f i r s t 1 2 H e l l o s e c o n d 2 3 W o r l d I n[ 4 5 ] :D a t a F r a m e ( d a t a ,c o l u m n s = [ ' C ' ,' A ' ,' B ' ] ) C A B 0 H e l l o 1 2 1 W o r l d 2 3

Note: DataFrameisnotintendedtoworkexactlylikea2dimensionalNumPyndarray.

Fromalistofdicts
I n[ 4 6 ] :d a t a 2=[ { ' a ' :1 ,' b ' :2 } ,{ ' a ' :5 ,' b ' :1 0 ,' c ' :2 0 } ] I n[ 4 7 ] :D a t a F r a m e ( d a t a 2 ) a b c 0 1 2N a N 1 5 1 0 2 0 I n[ 4 8 ] :D a t a F r a m e ( d a t a 2 ,i n d e x = [ ' f i r s t ' ,' s e c o n d ' ] ) a b c f i r s t 1 2N a N s e c o n d 5 1 0 2 0
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 8/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

I n[ 4 9 ] :D a t a F r a m e ( d a t a 2 ,c o l u m n s = [ ' a ' ,' b ' ] ) a b 0 1 2 1 5 1 0

FromaSeries
TheresultwillbeaDataFramewiththesameindexastheinputSeries,andwithonecolumnwhose nameistheoriginalnameoftheSeries(onlyifnoothercolumnnameprovided). MissingData MuchmorewillbesaidonthistopicintheMissingdatasection.ToconstructaDataFramewith missingdata,usen p . n a n forthosevalueswhicharemissing.Alternatively,youmaypassa
n u m p y . M a s k e d A r r a y asthedataargumenttotheDataFrameconstructor,anditsmaskedentries

willbeconsideredmissing.

AlternateConstructors
DataFrame.from_dict
D a t a F r a m e . f r o m _ d i c t takesadictofdictsoradictofarraylikesequencesandreturnsa

DataFrame.ItoperatesliketheD a t a F r a m e constructorexceptfortheo r i e n t parameterwhichis ' c o l u m n s ' bydefault,butwhichcanbesetto' i n d e x ' inordertousethedictkeysasrowlabels. DataFrame.from_records


D a t a F r a m e . f r o m _ r e c o r d s takesalistoftuplesoranndarraywithstructureddtype.Works

analogouslytothenormalD a t a F r a m e constructor,exceptthatindexmaybebeaspecificfieldofthe structureddtypetouseastheindex.Forexample:

I n[ 5 0 ] :d a t a a r r a y ( [ ( 1 ,2 . 0 ,' H e l l o ' ) ,( 2 ,3 . 0 ,' W o r l d ' ) ] , d t y p e = [ ( ' A ' ,' < i 4 ' ) ,( ' B ' ,' < f 4 ' ) ,( ' C ' ,' S 1 0 ' ) ] ) I n[ 5 1 ] :D a t a F r a m e . f r o m _ r e c o r d s ( d a t a ,i n d e x = ' C ' ) A B C H e l l o 1 2 W o r l d 2 3

DataFrame.from_items
D a t a F r a m e . f r o m _ i t e m s worksanalogouslytotheformofthed i c t constructorthattakesa
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 9/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

sequenceof( k e y ,v a l u e ) pairs,wherethekeysarecolumn(orrow,inthecaseof
o r i e n t = ' i n d e x ' )names,andthevaluearethecolumnvalues(orrowvalues).Thiscanbeuseful

forconstructingaDataFramewiththecolumnsinaparticularorderwithouthavingtopassanexplicit listofcolumns:

I n[ 5 2 ] :D a t a F r a m e . f r o m _ i t e m s ( [ ( ' A ' ,[ 1 ,2 ,3 ] ) ,( ' B ' ,[ 4 ,5 ,6 ] ) ] ) A B 0 1 4 1 2 5 2 3 6

Ifyoupasso r i e n t = ' i n d e x ' ,thekeyswillbetherowlabels.Butinthiscaseyoumustalsopassthe desiredcolumnnames:

I n[ 5 3 ] :D a t a F r a m e . f r o m _ i t e m s ( [ ( ' A ' ,[ 1 ,2 ,3 ] ) ,( ' B ' ,[ 4 ,5 ,6 ] ) ] , . . . . : o r i e n t = ' i n d e x ' ,c o l u m n s = [ ' o n e ' ,' t w o ' ,' t h r e e ' ] ) . . . . : A B o n e t w o t h r e e 1 2 3 4 5 6

Columnselection,addition,deletion
YoucantreataDataFramesemanticallylikeadictoflikeindexedSeriesobjects.Getting,setting, anddeletingcolumnsworkswiththesamesyntaxastheanalogousdictoperations:

I n[ 5 4 ] :d f [ ' o n e ' ] a 1 b 2 c 3 d N a N N a m e :o n e ,d t y p e :f l o a t 6 4 I n[ 5 5 ] :d f [ ' t h r e e ' ]=d f [ ' o n e ' ]*d f [ ' t w o ' ] I n[ 5 6 ] :d f [ ' f l a g ' ]=d f [ ' o n e ' ]>2 I n[ 5 7 ] :d f o n e t w o t h r e e f l a g a 1 1 1 F a l s e b 2 2 4 F a l s e c 3 3 9 T r u e d N a N 4 N a N F a l s e

pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe

10/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

Columnscanbedeletedorpoppedlikewithadict:

I n[ 5 8 ] :d e ld f [ ' t w o ' ] I n[ 5 9 ] :t h r e e=d f . p o p ( ' t h r e e ' ) I n[ 6 0 ] :d f o n e f l a g a 1 F a l s e b 2 F a l s e c 3 T r u e d N a N F a l s e

Wheninsertingascalarvalue,itwillnaturallybepropagatedtofillthecolumn:

I n[ 6 1 ] :d f [ ' f o o ' ]=' b a r ' I n[ 6 2 ] :d f o n e f l a g f o o a 1 F a l s e b a r b 2 F a l s e b a r c 3 T r u e b a r d N a N F a l s e b a r

WheninsertingaSeriesthatdoesnothavethesameindexastheDataFrame,itwillbeconformed totheDataFramesindex:

I n[ 6 3 ] :d f [ ' o n e _ t r u n c ' ]=d f [ ' o n e ' ] [ : 2 ] I n[ 6 4 ] :d f o n e f l a g f o o o n e _ t r u n c a 1 F a l s e b a r 1 b 2 F a l s e b a r 2 c 3 T r u e b a r N a N d N a N F a l s e b a r N a N

YoucaninsertrawndarraysbuttheirlengthmustmatchthelengthoftheDataFramesindex. Bydefault,columnsgetinsertedattheend.Thei n s e r t functionisavailabletoinsertataparticular locationinthecolumns:

I n[ 6 5 ] :d f . i n s e r t ( 1 ,' b a r ' ,d f [ ' o n e ' ] ) I n[ 6 6 ] :d f a b o n e b a r f l a g f o o o n e _ t r u n c 1 1 F a l s e b a r 1 2 2 F a l s e b a r 2


11/28

pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

c 3 3 T r u e b a r d N a N N a N F a l s e b a r

N a N N a N

Indexing/Selection
Thebasicsofindexingareasfollows: Operation Selectcolumn Selectrowbylabel Selectrowbyintegerlocation Slicerows Selectrowsbybooleanvector Syntax
d f [ c o l ] d f . l o c [ l a b e l ] d f . i l o c [ l o c ] d f [ 5 : 1 0 ] d f [ b o o l _ v e c ]

Result Series Series Series DataFrame DataFrame

Rowselection,forexample,returnsaSerieswhoseindexisthecolumnsoftheDataFrame:

I n[ 6 7 ] :d f . l o c [ ' b ' ] o n e 2 b a r 2 f l a g F a l s e f o o b a r o n e _ t r u n c 2 N a m e :b ,d t y p e :o b j e c t I n[ 6 8 ] :d f . i l o c [ 2 ] o n e 3 b a r 3 f l a g T r u e f o o b a r o n e _ t r u n c N a N N a m e :c ,d t y p e :o b j e c t

Foramoreexhaustivetreatmentofmoresophisticatedlabelbasedindexingandslicing,seethe sectiononindexing.Wewilladdressthefundamentalsofreindexing/conformingtonewsetsof lablesinthesectiononreindexing.

Dataalignmentandarithmetic
DataalignmentbetweenDataFrameobjectsautomaticallyalignonboththecolumnsandthe index(rowlabels).Again,theresultingobjectwillhavetheunionofthecolumnandrowlabels.

I n[ 6 9 ] :d f=D a t a F r a m e ( r a n d n ( 1 0 ,4 ) ,c o l u m n s = [ ' A ' ,' B ' ,' C ' ,' D ' ] ) I n[ 7 0 ] :d f 2=D a t a F r a m e ( r a n d n ( 7 ,3 ) ,c o l u m n s = [ ' A ' ,' B ' ,' C ' ] ) I n[ 7 1 ] :d f+d f 2
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 12/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

A B C D 01 . 4 7 30 . 6 2 60 . 7 7 3N a N 1 0 . 0 7 30 . 5 1 9 2 . 7 4 2N a N 2 1 . 7 4 41 . 3 2 5 0 . 0 7 5N a N 31 . 3 6 61 . 2 3 81 . 7 8 2N a N 4 0 . 2 7 50 . 6 1 32 . 2 6 3N a N 5 1 . 2 6 3 2 . 3 3 8 1 . 2 6 0N a N 61 . 2 1 6 3 . 3 7 11 . 9 9 2N a N 7 N a N N a N N a NN a N 8 N a N N a N N a NN a N 9 N a N N a N N a NN a N

WhendoinganoperationbetweenDataFrameandSeries,thedefaultbehavioristoaligntheSeries indexontheDataFramecolumns,thusbroadcastingrowwise.Forexample:

I n[ 7 2 ] :d f-d f . i l o c [ 0 ] A B C D 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 0 . 0 0 0 1 1 . 1 6 81 . 2 0 0 3 . 4 8 9 0 . 5 3 6 2 1 . 7 0 31 . 1 6 4 0 . 6 9 70 . 4 8 5 3 1 . 1 7 6 0 . 1 3 8 0 . 0 9 60 . 9 7 2 40 . 8 2 5 1 . 1 3 60 . 5 1 42 . 3 0 9 5 1 . 9 7 0 1 . 0 3 0 1 . 4 9 30 . 0 2 0 61 . 8 4 9 0 . 9 8 11 . 0 8 41 . 3 0 6 7 0 . 2 8 4 0 . 5 5 20 . 2 9 62 . 1 2 3 8 1 . 1 3 21 . 2 7 5 0 . 1 9 51 . 0 1 7 9 0 . 2 6 5 0 . 7 0 2 1 . 2 6 5 0 . 0 6 4

Inthespecialcaseofworkingwithtimeseriesdata,iftheSeriesisaTimeSeries(whichitwillbe automaticallyiftheindexcontainsdatetimeobjects),andtheDataFrameindexalsocontainsdates, thebroadcastingwillbecolumnwise:

I n[ 7 3 ] :i n d e x=d a t e _ r a n g e ( ' 1 / 1 / 2 0 0 0 ' ,p e r i o d s = 8 ) I n[ 7 4 ] :d f=D a t a F r a m e ( r a n d n ( 8 ,3 ) ,i n d e x = i n d e x ,c o l u m n s = l i s t ( ' A B C ' ) ) I n[ 7 5 ] :d f A B C 2 0 0 0 0 1 0 1 3 . 3 5 70 . 3 1 71 . 2 3 6 2 0 0 0 0 1 0 2 0 . 8 9 60 . 4 8 80 . 0 8 2 2 0 0 0 0 1 0 32 . 1 8 3 0 . 3 8 0 0 . 0 8 5 2 0 0 0 0 1 0 4 0 . 4 3 2 1 . 5 2 00 . 4 9 4 2 0 0 0 0 1 0 5 0 . 6 0 0 0 . 2 7 4 0 . 1 3 3 2 0 0 0 0 1 0 60 . 0 2 4 2 . 4 1 0 1 . 4 5 1 2 0 0 0 0 1 0 7 0 . 2 0 60 . 2 5 22 . 2 1 4 2 0 0 0 0 1 0 8 1 . 0 6 3 1 . 2 6 6 0 . 2 9 9 I n[ 7 6 ] :t y p e ( d f [ ' A ' ] ) p a n d a s . c o r e . s e r i e s . T i m e S e r i e s I n[ 7 7 ] :d f-d f [ ' A ' ] A B C
13/28

pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

2 0 0 0 0 1 0 1 2 0 0 0 0 1 0 2 2 0 0 0 0 1 0 3 2 0 0 0 0 1 0 4 2 0 0 0 0 1 0 5 2 0 0 0 0 1 0 6 2 0 0 0 0 1 0 7 2 0 0 0 0 1 0 8

03 . 6 7 54 . 5 9 4 01 . 3 8 40 . 9 7 8 0 2 . 5 6 3 2 . 2 6 8 0 1 . 0 8 80 . 9 2 6 00 . 3 2 60 . 4 6 7 0 2 . 4 3 4 1 . 4 7 4 00 . 4 5 82 . 4 2 0 0 0 . 2 0 30 . 7 6 4

Warning:

d f-d f [ ' A ' ]

isnowdeprecatedandwillberemovedinafuturerelease.Thepreferredwaytoreplicatethis behavioris

d f . s u b ( d f [ ' A ' ] ,a x i s = 0 )

Forexplicitcontroloverthematchingandbroadcastingbehavior,seethesectiononflexiblebinary operations. Operationswithscalarsarejustasyouwouldexpect:

I n[ 7 8 ] :d f*5+2 A B C 2 0 0 0 0 1 0 1 1 8 . 7 8 7 0 . 4 1 34 . 1 8 1 2 0 0 0 0 1 0 2 6 . 4 8 1 0 . 4 3 8 1 . 5 8 9 2 0 0 0 0 1 0 3 8 . 9 1 5 3 . 9 0 2 2 . 4 2 4 2 0 0 0 0 1 0 4 4 . 1 6 2 9 . 6 0 00 . 4 6 8 2 0 0 0 0 1 0 5 5 . 0 0 1 3 . 3 7 1 2 . 6 6 4 2 0 0 0 0 1 0 6 1 . 8 8 2 1 4 . 0 5 1 9 . 2 5 3 2 0 0 0 0 1 0 7 3 . 0 3 0 0 . 7 4 09 . 0 6 8 2 0 0 0 0 1 0 8 7 . 3 1 7 8 . 3 3 1 3 . 4 9 7 I n[ 7 9 ] :1/d f A B C 2 0 0 0 0 1 0 1 0 . 2 9 83 . 1 5 0 0 . 8 0 9 2 0 0 0 0 1 0 2 1 . 1 1 62 . 0 5 11 2 . 1 5 9 2 0 0 0 0 1 0 3 0 . 4 5 8 2 . 6 2 9 1 1 . 7 8 6 2 0 0 0 0 1 0 4 2 . 3 1 3 0 . 6 5 8 2 . 0 2 6 2 0 0 0 0 1 0 5 1 . 6 6 6 3 . 6 4 7 7 . 5 2 5 2 0 0 0 0 1 0 64 2 . 2 1 5 0 . 4 1 5 0 . 6 8 9 2 0 0 0 0 1 0 7 4 . 8 5 33 . 9 7 0 0 . 4 5 2 2 0 0 0 0 1 0 8 0 . 9 4 0 0 . 7 9 0 3 . 3 4 0 I n[ 8 0 ] :d f* *4 A
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe

C
14/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

2 0 0 0 0 1 0 1 2 0 0 0 0 1 0 2 2 0 0 0 0 1 0 3 2 0 0 0 0 1 0 4 2 0 0 0 0 1 0 5 2 0 0 0 0 1 0 6 2 0 0 0 0 1 0 7 2 0 0 0 0 1 0 8

1 . 2 7 1 e + 0 2 0 . 0 1 0 6 . 4 5 0 e 0 1 0 . 0 5 7 2 . 2 7 1 e + 0 1 0 . 0 2 1 3 . 4 9 5 e 0 2 5 . 3 3 8 1 . 2 9 8 e 0 1 0 . 0 0 6 3 . 1 4 9 e 0 7 3 3 . 7 4 4 1 . 8 0 3 e 0 3 0 . 0 0 4 1 . 2 7 8 e + 0 0 2 . 5 7 0

2 . 3 3 6 e + 0 0 4 . 5 7 4 e 0 5 5 . 1 8 2 e 0 5 5 . 9 3 9 e 0 2 3 . 1 1 8 e 0 4 4 . 4 2 7 e + 0 0 2 . 4 0 1 e + 0 1 8 . 0 3 2 e 0 3

Booleanoperatorsworkaswell:

I n[ 8 1 ] :d f 1=D a t a F r a m e ( { ' a ':[ 1 ,0 ,1 ] ,' b ':[ 0 ,1 ,1 ]} ,d t y p e = b o o l ) I n[ 8 2 ] :d f 2=D a t a F r a m e ( { ' a ':[ 0 ,1 ,1 ] ,' b ':[ 1 ,1 ,0 ]} ,d t y p e = b o o l ) I n[ 8 3 ] :d f 1&d f 2 a b 0 F a l s e F a l s e 1 F a l s e T r u e 2 T r u e F a l s e I n[ 8 4 ] :d f 1|d f 2 a b 0 T r u e T r u e 1 T r u e T r u e 2 T r u e T r u e I n[ 8 5 ] :d f 1^d f 2 a b 0 T r u e T r u e 1 T r u e F a l s e 2 F a l s e T r u e I n[ 8 6 ] :d f 1 a b 0 F a l s e T r u e 1 T r u e F a l s e 2 F a l s e F a l s e

Transposing
Totranspose,accesstheT attribute(alsothet r a n s p o s e function),similartoanndarray:
#o n l ys h o wt h ef i r s t5r o w s I n[ 8 7 ] :d f [ : 5 ] . T A B C 2 0 0 0 0 1 0 1 2 0 0 0 0 1 0 2 2 0 0 0 0 1 0 3 2 0 0 0 0 1 0 4 2 0 0 0 0 1 0 5 3 . 3 5 7 0 . 8 9 6 2 . 1 8 3 0 . 4 3 2 0 . 6 0 0 0 . 3 1 7 0 . 4 8 8 0 . 3 8 0 1 . 5 2 0 0 . 2 7 4 1 . 2 3 6 0 . 0 8 2 0 . 0 8 5 0 . 4 9 4 0 . 1 3 3
15/28

pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

DataFrameinteroperabilitywithNumPyfunctions
ElementwiseNumPyufuncs(log,exp,sqrt,...)andvariousotherNumPyfunctionscanbeusedwith noissuesonDataFrame,assumingthedatawithinarenumeric:

I n[ 8 8 ] :n p . e x p ( d f ) A B 2 0 0 0 0 1 0 1 2 8 . 7 1 5 0 . 7 2 8 2 0 0 0 0 1 0 2 2 . 4 5 0 0 . 6 1 4 2 0 0 0 0 1 0 3 0 . 1 1 3 1 . 4 6 3 2 0 0 0 0 1 0 4 1 . 5 4 1 4 . 5 7 2 2 0 0 0 0 1 0 5 1 . 8 2 2 1 . 3 1 6 2 0 0 0 0 1 0 6 0 . 9 7 7 1 1 . 1 3 6 2 0 0 0 0 1 0 7 1 . 2 2 9 0 . 7 7 7 2 0 0 0 0 1 0 8 2 . 8 9 6 3 . 5 4 7 I n[ 8 9 ] :n p . a s a r r a y ( d f ) a r r a y ( [ [3 . 3 5 7 4 ,0 . 3 1 7 4 ,1 . 2 3 6 3 ] , [0 . 8 9 6 2 ,0 . 4 8 7 6 ,0 . 0 8 2 2 ] , [ 2 . 1 8 2 9 , 0 . 3 8 0 4 , 0 . 0 8 4 8 ] , [0 . 4 3 2 4 , 1 . 5 2 ,0 . 4 9 3 7 ] , [0 . 6 0 0 2 , 0 . 2 7 4 2 , 0 . 1 3 2 9 ] , [ 0 . 0 2 3 7 , 2 . 4 1 0 2 , 1 . 4 5 0 5 ] , [0 . 2 0 6 1 ,0 . 2 5 1 9 ,2 . 2 1 3 6 ] , [1 . 0 6 3 3 , 1 . 2 6 6 1 , 0 . 2 9 9 4 ] ] ) C 0 . 2 9 0 0 . 9 2 1 1 . 0 8 9 0 . 6 1 0 1 . 1 4 2 4 . 2 6 5 0 . 1 0 9 1 . 3 4 9

ThedotmethodonDataFrameimplementsmatrixmultiplication:

I n[ 9 0 ] :d f . T . d o t ( d f ) A B C A 1 8 . 5 6 2 0 . 2 7 44 . 7 1 5 B 0 . 2 7 4 1 0 . 3 4 4 4 . 1 8 4 C 4 . 7 1 5 4 . 1 8 4 8 . 8 9 7

Similarly,thedotmethodonSeriesimplementsdotproduct:

I n[ 9 1 ] :s 1=S e r i e s ( n p . a r a n g e ( 5 , 1 0 ) ) I n[ 9 2 ] :s 1 . d o t ( s 1 ) 2 5 5

DataFrameisnotintendedtobeadropinreplacementforndarrayasitsindexingsemanticsare quitedifferentinplacesfromamatrix.

Consoledisplay
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 16/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

ForverylargeDataFrameobjects,onlyasummarywillbeprintedtotheconsole(hereIamreading aCSVversionofthebaseballdatasetfromtheplyrRpackage):

I n[ 9 3 ] :b a s e b a l l=r e a d _ c s v ( ' d a t a / b a s e b a l l . c s v ' ) I n[ 9 4 ] :p r i n tb a s e b a l l < c l a s s' p a n d a s . c o r e . f r a m e . D a t a F r a m e ' > I n t 6 4 I n d e x :1 0 0e n t r i e s ,8 8 6 4 1t o8 9 5 3 4 D a t ac o l u m n s( t o t a l2 2c o l u m n s ) : i d 1 0 0 n o n n u l lv a l u e s y e a r 1 0 0 n o n n u l lv a l u e s s t i n t 1 0 0 n o n n u l lv a l u e s t e a m 1 0 0 n o n n u l lv a l u e s l g 1 0 0 n o n n u l lv a l u e s g 1 0 0 n o n n u l lv a l u e s a b 1 0 0 n o n n u l lv a l u e s r 1 0 0 n o n n u l lv a l u e s h 1 0 0 n o n n u l lv a l u e s X 2 b 1 0 0 n o n n u l lv a l u e s X 3 b 1 0 0 n o n n u l lv a l u e s h r 1 0 0 n o n n u l lv a l u e s r b i 1 0 0 n o n n u l lv a l u e s s b 1 0 0 n o n n u l lv a l u e s c s 1 0 0 n o n n u l lv a l u e s b b 1 0 0 n o n n u l lv a l u e s s o 1 0 0 n o n n u l lv a l u e s i b b 1 0 0 n o n n u l lv a l u e s h b p 1 0 0 n o n n u l lv a l u e s s h 1 0 0 n o n n u l lv a l u e s s f 1 0 0 n o n n u l lv a l u e s g i d p 1 0 0 n o n n u l lv a l u e s d t y p e s :f l o a t 6 4 ( 9 ) ,i n t 6 4 ( 1 0 ) ,o b j e c t ( 3 )

However,usingt o _ s t r i n g willreturnastringrepresentationoftheDataFrameintabularform, thoughitwontalwaysfittheconsolewidth:

I n[ 9 5 ] :p r i n tb a s e b a l l . i l o c [ 2 0 : ,: 1 2 ] . t o _ s t r i n g ( ) i d y e a r s t i n tt e a m l g g a b r h X 2 b X 3 b h r 8 9 4 7 4 f i n l e s t 0 1 2 0 0 7 1 C O L N L 4 3 9 4 9 1 7 3 0 1 8 9 4 8 0 e m b r e a l 0 1 2 0 0 7 1 O A K A L 4 0 0 0 0 0 0 8 9 4 8 1 e d m o n j i 0 1 2 0 0 7 1 S L N N L 1 1 7 3 6 5 3 9 9 2 1 5 2 1 2 8 9 4 8 2 e a s l e d a 0 1 2 0 0 7 1 N Y N N L 7 6 1 9 3 2 4 5 4 6 0 1 0 8 9 4 8 9 d e l g a c a 0 1 2 0 0 7 1 N Y N N L 1 3 9 5 3 8 7 1 1 3 9 3 0 0 2 4 8 9 4 9 3 c o r m i r h 0 1 2 0 0 7 1 C I N N L 6 0 0 0 0 0 0 8 9 4 9 4 c o n i n j e 0 1 2 0 0 7 2 N Y N N L 2 1 4 1 2 8 2 0 0 8 9 4 9 5 c o n i n j e 0 1 2 0 0 7 1 C I N N L 8 0 2 1 5 2 3 5 7 1 1 1 6 8 9 4 9 7 c l e m e r o 0 2 2 0 0 7 1 N Y A A L 2 2 0 1 0 0 0 8 9 4 9 8 c l a y t r o 0 1 2 0 0 7 2 B O S A L 8 6 1 0 0 0 0 8 9 4 9 9 c l a y t r o 0 1 2 0 0 7 1 T O R A L 6 9 1 8 9 2 3 4 8 1 4 0 1 8 9 5 0 1 c i r i l j e 0 1 2 0 0 7 2 A R I N L 2 8 4 0 6 8 4 0 0 8 9 5 0 2 c i r i l j e 0 1 2 0 0 7 1 M I N A L 5 0 1 5 3 1 8 4 0 9 2 2 8 9 5 2 1 b o n d s b a 0 1 2 0 0 7 1 S F N N L 1 2 6 3 4 0 7 5 9 4 1 4 0 2 8 8 9 5 2 3 b i g g i c r 0 1 2 0 0 7 1 H O U N L 1 4 1 5 1 7 6 8 1 3 0 3 1 3 1 0 8 9 5 2 5 b e n i t a r 0 1 2 0 0 7 2 F L O N L 3 4 0 0 0 0 0 0 8 9 5 2 6 b e n i t a r 0 1 2 0 0 7 1 S F N N L 1 9 0 0 0 0 0 0 8 9 5 3 0 a u s m u b r 0 1 2 0 0 7 1 H O U N L 1 1 7 3 4 9 3 8 8 2 1 6 3 3
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 17/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

8 9 5 3 3 a l o u m o 0 1 2 0 0 7 8 9 5 3 4 a l o m a s a 0 2 2 0 0 7

1 N Y N N L 1 N Y N N L

8 7 3 2 8 5 1 1 1 2 8 2 2 1 3

1 9 1

1 1 3 0 0

Newsince0.10.0,wideDataFrameswillnowbeprintedacrossmultiplerowsbydefault:

I n[ 9 6 ] :D a t a F r a m e ( r a n d n ( 3 ,1 2 ) ) 0 1 2 3 4 5 6 \ 00 . 8 6 3 8 3 8 0 . 4 0 8 2 0 41 . 0 4 8 0 8 90 . 0 2 5 7 4 70 . 9 8 8 3 8 7 0 . 0 9 4 0 5 5 1 . 2 6 2 7 3 1 1 0 . 3 6 9 3 7 40 . 0 3 4 5 7 12 . 4 8 4 4 7 80 . 2 8 1 4 6 1 0 . 0 3 0 7 1 1 0 . 1 0 9 1 2 1 1 . 1 2 6 2 0 3 21 . 0 7 1 3 5 7 0 . 4 4 1 1 5 3 2 . 3 5 3 9 2 5 0 . 5 8 3 7 8 7 0 . 2 2 1 4 7 10 . 7 4 4 4 7 1 0 . 7 5 8 5 2 7 7 8 9 1 0 1 1 0 1 . 2 8 9 9 9 7 0 . 0 8 2 4 2 30 . 0 5 5 7 5 8 0 . 5 3 6 5 8 00 . 4 8 9 6 8 2 10 . 9 7 7 3 4 9 1 . 4 7 4 0 7 10 . 0 6 4 0 3 41 . 2 8 2 7 8 2 0 . 7 8 1 8 3 6 2 1 . 7 2 9 6 8 90 . 9 6 4 9 8 00 . 8 4 5 6 9 61 . 3 4 0 8 9 6 1 . 8 4 6 8 8 3

Youcanchangehowmuchtoprintonasinglerowbysettingthel i n e _ w i d t h option:
I n[ 9 7 ] :s e t _ o p t i o n ( ' l i n e _ w i d t h ' ,4 0 )#d e f a u l ti s8 0 I n[ 9 8 ] :D a t a F r a m e ( r a n d n ( 3 ,1 2 ) ) 0 1 2 \ 01 . 3 2 8 8 6 5 1 . 6 8 2 7 0 61 . 7 1 7 6 9 3 1 0 . 3 0 6 9 9 60 . 0 2 8 6 6 5 0 . 3 8 4 3 1 6 21 . 1 3 7 7 0 70 . 8 9 1 0 6 00 . 6 9 3 9 2 1 3 4 5 \ 0 0 . 8 8 8 7 8 2 0 . 2 2 8 4 4 0 0 . 9 0 1 8 0 5 1 1 . 5 7 4 1 5 9 1 . 5 8 8 9 3 1 0 . 4 7 6 7 2 0 2 1 . 6 1 3 6 1 6 0 . 4 6 4 0 0 0 0 . 2 2 7 3 7 1 6 7 8 \ 0 1 . 1 7 1 2 1 6 0 . 5 2 0 2 6 01 . 1 9 7 0 7 1 1 0 . 4 7 3 4 2 40 . 2 4 2 8 6 10 . 0 1 4 8 0 5 20 . 4 9 6 9 2 2 0 . 3 0 6 3 8 92 . 2 9 0 6 1 3 9 1 0 1 1 01 . 0 6 6 9 6 90 . 3 0 3 4 2 10 . 8 5 8 4 4 7 10 . 2 8 4 3 1 9 0 . 6 5 0 7 7 61 . 4 6 1 6 6 5 21 . 1 3 4 6 2 31 . 5 6 1 8 1 90 . 2 6 0 8 3 8

Youcanalsodisablethisfeatureviathee x p a n d _ f r a m e _ r e p r option:

I n[ 9 9 ] :s e t _ o p t i o n ( ' e x p a n d _ f r a m e _ r e p r ' ,F a l s e ) I n[ 1 0 0 ] :D a t a F r a m e ( r a n d n ( 3 ,1 2 ) ) < c l a s s' p a n d a s . c o r e . f r a m e . D a t a F r a m e ' > I n t 6 4 I n d e x :3e n t r i e s ,0t o2 D a t ac o l u m n s( t o t a l1 2c o l u m n s ) : 0 3 n o n n u l lv a l u e s 1 3 n o n n u l lv a l u e s 2 3 n o n n u l lv a l u e s 3 3 n o n n u l lv a l u e s 4 3 n o n n u l lv a l u e s 5 3 n o n n u l lv a l u e s


pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 18/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

6 3 n o n n u l lv a l u e s 7 3 n o n n u l lv a l u e s 8 3 n o n n u l lv a l u e s 9 3 n o n n u l lv a l u e s 1 0 3 n o n n u l lv a l u e s 1 1 3 n o n n u l lv a l u e s d t y p e s :f l o a t 6 4 ( 1 2 )

DataFramecolumnattributeaccessandIPythoncompletion
IfaDataFramecolumnlabelisavalidPythonvariablename,thecolumncanbeaccessedlike attributes:

I n[ 1 0 1 ] :d f=D a t a F r a m e ( { ' f o o 1 ':n p . r a n d o m . r a n d n ( 5 ) , . . . . . : ' f o o 2 ':n p . r a n d o m . r a n d n ( 5 ) } ) . . . . . : I n[ 1 0 2 ] :d f f o o 1 f o o 2 0 0 . 9 6 7 6 6 10 . 6 8 1 0 8 7 11 . 0 5 7 9 0 9 0 . 3 7 7 9 5 3 2 1 . 3 7 5 0 2 0 0 . 4 9 3 6 7 2 30 . 9 2 8 7 9 72 . 4 6 1 4 6 7 40 . 3 0 8 8 5 31 . 5 5 3 9 0 2 I n[ 1 0 3 ] :d f . f o o 1 0 0 . 9 6 7 6 6 1 1 1 . 0 5 7 9 0 9 2 1 . 3 7 5 0 2 0 3 0 . 9 2 8 7 9 7 4 0 . 3 0 8 8 5 3 N a m e :f o o 1 ,d t y p e :f l o a t 6 4

ThecolumnsarealsoconnectedtotheIPythoncompletionmechanismsotheycanbetab completed:

I n[ 5 ] :d f . f o < T A B > d f . f o o 1 d f . f o o 2

Panel
Panelisasomewhatlessused,butstillimportantcontainerfor3dimensionaldata.Thetermpanel dataisderivedfromeconometricsandispartiallyresponsibleforthenamepandas:pan(el)da(ta)s. Thenamesforthe3axesareintendedtogivesomesemanticmeaningtodescribingoperations involvingpaneldataand,inparticular,econometricanalysisofpaneldata.However,forthestrict purposesofslicinganddicingacollectionofDataFrameobjects,youmayfindtheaxisnames
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 19/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

slightlyarbitrary: items:axis0,eachitemcorrespondstoaDataFramecontainedinside major_axis:axis1,itistheindex(rows)ofeachoftheDataFrames minor_axis:axis2,itisthecolumnsofeachoftheDataFrames ConstructionofPanelsworksaboutlikeyouwouldexpect:

From3Dndarraywithoptionalaxislabels
I n[ 1 0 4 ] :w p=P a n e l ( r a n d n ( 2 ,5 ,4 ) ,i t e m s = [ ' I t e m 1 ' ,' I t e m 2 ' ] , . . . . . : m a j o r _ a x i s = d a t e _ r a n g e ( ' 1 / 1 / 2 0 0 0 ' ,p e r i o d s = 5 ) , . . . . . : m i n o r _ a x i s = [ ' A ' ,' B ' ,' C ' ,' D ' ] ) . . . . . : I n[ 1 0 5 ] :w p < c l a s s' p a n d a s . c o r e . p a n e l . P a n e l ' > D i m e n s i o n s :2( i t e m s )x5( m a j o r _ a x i s )x4( m i n o r _ a x i s ) I t e m sa x i s :I t e m 1t oI t e m 2 M a j o r _ a x i sa x i s :2 0 0 0 0 1 0 10 0 : 0 0 : 0 0t o2 0 0 0 0 1 0 50 0 : 0 0 : 0 0 M i n o r _ a x i sa x i s :At oD

FromdictofDataFrameobjects
I n[ 1 0 6 ] :d a t a={ ' I t e m 1 ':D a t a F r a m e ( r a n d n ( 4 ,3 ) ) , . . . . . : ' I t e m 2 ':D a t a F r a m e ( r a n d n ( 4 ,2 ) ) } . . . . . : I n[ 1 0 7 ] :P a n e l ( d a t a ) < c l a s s' p a n d a s . c o r e . p a n e l . P a n e l ' > D i m e n s i o n s :2( i t e m s )x4( m a j o r _ a x i s )x3( m i n o r _ a x i s ) I t e m sa x i s :I t e m 1t oI t e m 2 M a j o r _ a x i sa x i s :0t o3 M i n o r _ a x i sa x i s :0t o2

NotethatthevaluesinthedictneedonlybeconvertibletoDataFrame.Thus,theycanbeanyof theothervalidinputstoDataFrameasperabove. OnehelpfulfactorymethodisP a n e l . f r o m _ d i c t ,whichtakesadictionaryofDataFramesasabove, andthefollowingnamedparameters: Parameter intersect orient Default


F a l s e i t e m s

Description dropselementswhoseindicesdonotalign usem i n o r touseDataFramescolumnsaspanelitems

Forexample,comparetotheconstructionabove:
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 20/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

I n[ 1 0 8 ] :P a n e l . f r o m _ d i c t ( d a t a ,o r i e n t = ' m i n o r ' ) < c l a s s' p a n d a s . c o r e . p a n e l . P a n e l ' > D i m e n s i o n s :3( i t e m s )x4( m a j o r _ a x i s )x2( m i n o r _ a x i s ) I t e m sa x i s :0t o2 M a j o r _ a x i sa x i s :0t o3 M i n o r _ a x i sa x i s :I t e m 1t oI t e m 2

OrientisespeciallyusefulformixedtypeDataFrames.IfyoupassadictofDataFrameobjectswith mixedtypecolumns,allofthedatawillgetupcastedtod t y p e = o b j e c t unlessyoupass


o r i e n t = ' m i n o r ' :

I n[ 1 0 9 ] :d f=D a t a F r a m e ( { ' a ' :[ ' f o o ' ,' b a r ' ,' b a z ' ] , . . . . . : ' b ' :n p . r a n d o m . r a n d n ( 3 ) } ) . . . . . : I n[ 1 1 0 ] :d f a b 0 f o o1 . 0 0 4 1 6 8 1 b a r1 . 3 7 7 6 2 7 2 b a z 0 . 4 9 9 2 8 1 I n[ 1 1 1 ] :d a t a={ ' i t e m 1 ' :d f ,' i t e m 2 ' :d f } I n[ 1 1 2 ] :p a n e l=P a n e l . f r o m _ d i c t ( d a t a ,o r i e n t = ' m i n o r ' ) I n[ 1 1 3 ] :p a n e l [ ' a ' ] 0 1 2 i t e m 1i t e m 2 f o o f o o b a r b a r b a z b a z

I n[ 1 1 4 ] :p a n e l [ ' b ' ] i t e m 1 i t e m 2 01 . 0 0 4 1 6 81 . 0 0 4 1 6 8 11 . 3 7 7 6 2 71 . 3 7 7 6 2 7 2 0 . 4 9 9 2 8 1 0 . 4 9 9 2 8 1 I n[ 1 1 5 ] :p a n e l [ ' b ' ] . d t y p e s i t e m 1 f l o a t 6 4 i t e m 2 f l o a t 6 4 d t y p e :o b j e c t

Note: UnfortunatelyPanel,beinglesscommonlyusedthanSeriesandDataFrame,hasbeen slightlyneglectedfeaturewise.AnumberofmethodsandoptionsavailableinDataFramearenot availableinPanel.Thiswillgetworkedon,ofcourse,infuturereleases.Andfasterifyoujoinme inworkingonthecodebase.

pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe

21/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

FromDataFrameusingt o _ p a n e l method
Thismethodwasintroducedinv0.7toreplaceL o n g P a n e l . t o _ l o n g ,andconvertsaDataFrame withatwolevelindextoaPanel.

I n[ 1 1 6 ] :m i d x=M u l t i I n d e x ( l e v e l s = [ [ ' o n e ' ,' t w o ' ] ,[ ' x ' , ' y ' ] ] ,l a b e l s = [ [ 1 , 1 , 0 , 0 ] , [ I n[ 1 1 7 ] :d f=D a t a F r a m e ( { ' A ':[ 1 ,2 ,3 ,4 ] ,' B ' :[ 5 ,6 ,7 ,8 ] } ,i n d e x = m i d x ) I n[ 1 1 8 ] :d f . t o _ p a n e l ( ) < c l a s s' p a n d a s . c o r e . p a n e l . P a n e l ' > D i m e n s i o n s :2( i t e m s )x2( m a j o r _ a x i s )x2( m i n o r _ a x i s ) I t e m sa x i s :At oB M a j o r _ a x i sa x i s :o n et ot w o M i n o r _ a x i sa x i s :xt oy

Itemselection/addition/deletion
SimilartoDataFramefunctioningasadictofSeries,PanelislikeadictofDataFrames:

I n[ 1 1 9 ] :w p [ ' I t e m 1 ' ] A B C D 2 0 0 0 0 1 0 1 2 . 0 1 5 5 2 31 . 8 3 3 7 2 2 1 . 7 7 1 7 4 00 . 6 7 0 0 2 7 2 0 0 0 0 1 0 2 0 . 0 4 9 3 0 70 . 5 2 1 4 9 33 . 2 0 1 7 5 0 0 . 7 9 2 7 1 6 2 0 0 0 0 1 0 3 0 . 1 4 6 1 1 1 1 . 9 0 3 2 4 70 . 7 4 7 1 6 90 . 3 0 9 0 3 8 2 0 0 0 0 1 0 4 0 . 3 9 3 8 7 6 1 . 8 6 1 4 6 8 0 . 9 3 6 5 2 7 1 . 2 5 5 7 4 6 2 0 0 0 0 1 0 52 . 6 5 5 4 5 2 1 . 2 1 9 4 9 2 0 . 0 6 2 2 9 70 . 1 1 0 3 8 8 I n[ 1 2 0 ] :w p [ ' I t e m 3 ' ]=w p [ ' I t e m 1 ' ]/w p [ ' I t e m 2 ' ]

TheAPIforinsertionanddeletionisthesameasforDataFrame.AndaswithDataFrame,iftheitem isavalidpythonidentifier,youcanaccessitasanattributeandtabcompleteitinIPython.

Transposing
APanelcanberearrangedusingitst r a n s p o s e method(whichdoesnotmakeacopybydefault unlessthedataareheterogeneous):

I n[ 1 2 1 ] :w p . t r a n s p o s e ( 2 ,0 ,1 ) < c l a s s' p a n d a s . c o r e . p a n e l . P a n e l ' > D i m e n s i o n s :4( i t e m s )x3( m a j o r _ a x i s )x5( m i n o r _ a x i s ) I t e m sa x i s :At oD M a j o r _ a x i sa x i s :I t e m 1t oI t e m 3 M i n o r _ a x i sa x i s :2 0 0 0 0 1 0 10 0 : 0 0 : 0 0t o2 0 0 0 0 1 0 50 0 : 0 0 : 0 0


pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 22/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

Indexing/Selection
Operation Selectitem Getsliceatmajor_axislabel Getsliceatminor_axislabel Syntax
w p [ i t e m ] w p . m a j o r _ x s ( v a l ) w p . m i n o r _ x s ( v a l )

Result DataFrame DataFrame DataFrame

Forexample,usingtheearlierexampledata,wecoulddo:

I n[ 1 2 2 ] :w p [ ' I t e m 1 ' ] A B C D 2 0 0 0 0 1 0 1 2 . 0 1 5 5 2 31 . 8 3 3 7 2 2 1 . 7 7 1 7 4 00 . 6 7 0 0 2 7 2 0 0 0 0 1 0 2 0 . 0 4 9 3 0 70 . 5 2 1 4 9 33 . 2 0 1 7 5 0 0 . 7 9 2 7 1 6 2 0 0 0 0 1 0 3 0 . 1 4 6 1 1 1 1 . 9 0 3 2 4 70 . 7 4 7 1 6 90 . 3 0 9 0 3 8 2 0 0 0 0 1 0 4 0 . 3 9 3 8 7 6 1 . 8 6 1 4 6 8 0 . 9 3 6 5 2 7 1 . 2 5 5 7 4 6 2 0 0 0 0 1 0 52 . 6 5 5 4 5 2 1 . 2 1 9 4 9 2 0 . 0 6 2 2 9 70 . 1 1 0 3 8 8 I n[ 1 2 3 ] :w p . m a j o r _ x s ( w p . m a j o r _ a x i s [ 2 ] ) I t e m 1 I t e m 2 I t e m 3 A 0 . 1 4 6 1 1 11 . 1 3 9 0 5 00 . 1 2 8 2 7 5 B 1 . 9 0 3 2 4 7 0 . 6 6 0 3 4 2 2 . 8 8 2 2 1 4 C0 . 7 4 7 1 6 9 0 . 4 6 4 7 9 41 . 6 0 7 5 2 6 D0 . 3 0 9 0 3 80 . 3 0 9 3 3 7 0 . 9 9 9 0 3 5 I n[ 1 2 4 ] :w p . m i n o r _ a x i s I n d e x ( [ u ' A ' ,u ' B ' ,u ' C ' ,u ' D ' ] ,d t y p e = o b j e c t ) I n[ 1 2 5 ] :w p . m i n o r _ x s ( ' C ' ) I t e m 1 I t e m 2 2 0 0 0 0 1 0 1 1 . 7 7 1 7 4 0 0 . 0 7 7 8 4 9 2 0 0 0 0 1 0 23 . 2 0 1 7 5 0 0 . 5 0 3 7 0 3 2 0 0 0 0 1 0 30 . 7 4 7 1 6 9 0 . 4 6 4 7 9 4 2 0 0 0 0 1 0 4 0 . 9 3 6 5 2 70 . 6 4 3 8 3 4 2 0 0 0 0 1 0 5 0 . 0 6 2 2 9 7 0 . 7 8 7 8 7 2 I t e m 3 2 2 . 7 5 8 6 1 8 6 . 3 5 6 4 2 2 1 . 6 0 7 5 2 6 1 . 4 5 4 6 0 9 0 . 0 7 9 0 7 0

Squeezing
Anotherwaytochangethedimensionalityofanobjectistos q u e e z e a1lenobject,similarto
w p [ ' I t e m 1 ' ]

I n[ 1 2 6 ] :w p . r e i n d e x ( i t e m s = [ ' I t e m 1 ' ] ) . s q u e e z e ( ) A B C D 2 0 0 0 0 1 0 1 2 . 0 1 5 5 2 31 . 8 3 3 7 2 2 1 . 7 7 1 7 4 00 . 6 7 0 0 2 7 2 0 0 0 0 1 0 2 0 . 0 4 9 3 0 70 . 5 2 1 4 9 33 . 2 0 1 7 5 0 0 . 7 9 2 7 1 6 2 0 0 0 0 1 0 3 0 . 1 4 6 1 1 1 1 . 9 0 3 2 4 70 . 7 4 7 1 6 90 . 3 0 9 0 3 8 2 0 0 0 0 1 0 4 0 . 3 9 3 8 7 6 1 . 8 6 1 4 6 8 0 . 9 3 6 5 2 7 1 . 2 5 5 7 4 6 2 0 0 0 0 1 0 52 . 6 5 5 4 5 2 1 . 2 1 9 4 9 2 0 . 0 6 2 2 9 70 . 1 1 0 3 8 8 I n[ 1 2 7 ] :w p . r e i n d e x ( i t e m s = [ ' I t e m 1 ' ] , m i n o r = [ ' B ' ] ) . s q u e e z e ( )


pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 23/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

2 0 0 0 0 1 0 1 1 . 8 3 3 7 2 2 2 0 0 0 0 1 0 2 0 . 5 2 1 4 9 3 2 0 0 0 0 1 0 3 1 . 9 0 3 2 4 7 2 0 0 0 0 1 0 4 1 . 8 6 1 4 6 8 2 0 0 0 0 1 0 5 1 . 2 1 9 4 9 2 F r e q :D ,N a m e :B ,d t y p e :f l o a t 6 4

ConversiontoDataFrame
APanelcanberepresentedin2DformasahierarchicallyindexedDataFrame.Seethesection hierarchicalindexingformoreonthis.ToconvertaPaneltoaDataFrame,usethet o _ f r a m e method:

I n[ 1 2 8 ] :p a n e l=P a n e l ( n p . r a n d o m . r a n d n ( 3 ,5 ,4 ) ,i t e m s = [ ' o n e ' ,' t w o ' ,' t h r e e ' ] , . . . . . : m a j o r _ a x i s = d a t e _ r a n g e ( ' 1 / 1 / 2 0 0 0 ' ,p e r i o d s = 5 ) , . . . . . : m i n o r _ a x i s = [ ' a ' ,' b ' ,' c ' ,' d ' ] ) . . . . . : I n[ 1 2 9 ] :p a n e l . t o _ f r a m e ( ) o n e t w o t h r e e m a j o r m i n o r 2 0 0 0 0 1 0 1a 1 . 4 0 5 2 5 61 . 1 5 7 8 8 6 0 . 0 8 6 9 2 6 b 0 . 1 6 2 5 6 50 . 5 5 1 8 6 50 . 4 4 5 6 4 5 c 0 . 0 6 7 7 8 5 1 . 5 9 2 6 7 30 . 2 1 7 5 0 3 d 1 . 2 6 0 0 0 6 1 . 5 5 9 3 1 81 . 4 2 0 3 6 1 2 0 0 0 0 1 0 2a 1 . 1 3 2 8 9 6 1 . 5 6 2 4 4 30 . 0 1 5 6 0 1 b 2 . 0 0 6 4 8 1 0 . 7 6 3 2 6 41 . 1 5 0 6 4 1 c 0 . 3 0 1 0 1 6 0 . 1 6 2 0 2 70 . 7 9 8 3 3 4 d 0 . 0 5 9 1 1 70 . 9 0 2 7 0 40 . 5 5 7 6 9 7 2 0 0 0 0 1 0 3a 1 . 1 3 8 4 6 9 1 . 1 0 6 0 1 0 0 . 3 8 1 3 5 3 b 2 . 4 0 0 6 3 40 . 1 9 9 2 3 4 1 . 3 3 7 1 2 2 c 0 . 2 8 0 8 5 3 0 . 4 5 8 2 6 51 . 5 3 1 0 9 5 d 0 . 0 2 5 6 5 3 0 . 4 9 1 0 4 8 1 . 3 3 1 4 5 8 2 0 0 0 0 1 0 4a 1 . 3 8 6 0 7 1 0 . 1 2 8 5 9 40 . 5 7 1 3 2 9 b 0 . 8 6 3 9 3 7 1 . 1 4 7 8 6 20 . 0 2 6 6 7 1 c 0 . 2 5 2 4 6 21 . 2 5 6 8 6 01 . 0 8 5 6 6 3 d 1 . 5 0 0 5 7 1 0 . 5 6 3 6 3 71 . 1 1 4 7 3 8 2 0 0 0 0 1 0 5a 1 . 0 5 3 2 0 22 . 4 1 7 3 1 20 . 0 5 8 2 1 6 b 2 . 3 3 8 5 9 5 0 . 9 7 2 8 2 70 . 4 8 6 7 6 8 c 0 . 3 7 4 2 7 9 0 . 0 4 1 2 9 3 1 . 6 8 5 1 4 8 d 2 . 3 5 9 9 5 8 1 . 1 2 9 6 5 9 0 . 1 1 2 5 7 2

Panel4D(Experimental)
P a n e l 4 D isa4DimensionalnamedcontainerverymuchlikeaP a n e l ,buthaving4named

dimensions.ItisintendedasatestbedformoreNDimensionalnamedcontainers. labels:axis0,eachitemcorrespondstoaPanelcontainedinside
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 24/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

items:axis1,eachitemcorrespondstoaDataFramecontainedinside major_axis:axis2,itistheindex(rows)ofeachoftheDataFrames minor_axis:axis3,itisthecolumnsofeachoftheDataFrames


P a n e l 4 D isasubclassofP a n e l ,somostmethodsthatworkonPanelsareapplicabletoPanel4D.

Thefollowingmethodsaredisabled:
j o i n,t o _ f r a m e,t o _ e x c e l,t o _ s p a r s e,g r o u p b y

ConstructionofPanel4DworksinaverysimilarmannertoaP a n e l

From4Dndarraywithoptionalaxislabels
I n[ 1 3 0 ] :p 4 d=P a n e l 4 D ( r a n d n ( 2 ,2 ,5 ,4 ) , . . . . . : l a b e l s = [ ' L a b e l 1 ' , ' L a b e l 2 ' ] , . . . . . : i t e m s = [ ' I t e m 1 ' ,' I t e m 2 ' ] , . . . . . : m a j o r _ a x i s = d a t e _ r a n g e ( ' 1 / 1 / 2 0 0 0 ' ,p e r i o d s = 5 ) , . . . . . : m i n o r _ a x i s = [ ' A ' ,' B ' ,' C ' ,' D ' ] ) . . . . . : I n[ 1 3 1 ] :p 4 d < c l a s s' p a n d a s . c o r e . p a n e l n d . P a n e l 4 D ' > D i m e n s i o n s :2( l a b e l s )x2( i t e m s )x5( m a j o r _ a x i s )x4( m i n o r _ a x i s ) L a b e l sa x i s :L a b e l 1t oL a b e l 2 I t e m sa x i s :I t e m 1t oI t e m 2 M a j o r _ a x i sa x i s :2 0 0 0 0 1 0 10 0 : 0 0 : 0 0t o2 0 0 0 0 1 0 50 0 : 0 0 : 0 0 M i n o r _ a x i sa x i s :At oD

FromdictofPanelobjects
I n[ 1 3 2 ] :d a t a={' L a b e l 1 ':P a n e l ( {' I t e m 1 ':D a t a F r a m e ( r a n d n ( 4 ,3 ) )} ) , . . . . . : ' L a b e l 2 ':P a n e l ( {' I t e m 2 ':D a t a F r a m e ( r a n d n ( 4 ,2 ) )} )} . . . . . : I n[ 1 3 3 ] :P a n e l 4 D ( d a t a ) < c l a s s' p a n d a s . c o r e . p a n e l n d . P a n e l 4 D ' > D i m e n s i o n s :2( l a b e l s )x2( i t e m s )x4( m a j o r _ a x i s )x3( m i n o r _ a x i s ) L a b e l sa x i s :L a b e l 1t oL a b e l 2 I t e m sa x i s :I t e m 1t oI t e m 2 M a j o r _ a x i sa x i s :0t o3 M i n o r _ a x i sa x i s :0t o2

NotethatthevaluesinthedictneedonlybeconvertibletoPanels.Thus,theycanbeanyofthe othervalidinputstoPanelasperabove.

Slicing
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 25/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

SlicingworksinasimilarmannertoaPanel.[ ] slicesthefirstdimension.. i x allowsyoutoslice abitrarilyandgetbacklowerdimensionalobjects

I n[ 1 3 4 ] :p 4 d [ ' L a b e l 1 ' ] < c l a s s' p a n d a s . c o r e . p a n e l . P a n e l ' > D i m e n s i o n s :2( i t e m s )x5( m a j o r _ a x i s )x4( m i n o r _ a x i s ) I t e m sa x i s :I t e m 1t oI t e m 2 M a j o r _ a x i sa x i s :2 0 0 0 0 1 0 10 0 : 0 0 : 0 0t o2 0 0 0 0 1 0 50 0 : 0 0 : 0 0 M i n o r _ a x i sa x i s :At oD

4D>Panel

I n[ 1 3 5 ] :p 4 d . i x [ : , : , : , ' A ' ] < c l a s s' p a n d a s . c o r e . p a n e l . P a n e l ' > D i m e n s i o n s :2( i t e m s )x2( m a j o r _ a x i s )x5( m i n o r _ a x i s ) I t e m sa x i s :L a b e l 1t oL a b e l 2 M a j o r _ a x i sa x i s :I t e m 1t oI t e m 2 M i n o r _ a x i sa x i s :2 0 0 0 0 1 0 10 0 : 0 0 : 0 0t o2 0 0 0 0 1 0 50 0 : 0 0 : 0 0

4D>DataFrame

I n[ 1 3 6 ] :p 4 d . i x [ : , : , 0 , ' A ' ] L a b e l 1 L a b e l 2 I t e m 11 . 4 9 5 3 0 90 . 7 3 9 7 7 6 I t e m 2 1 . 1 0 3 9 4 9 0 . 4 0 3 7 7 6

4D>Series

I n[ 1 3 7 ] :p 4 d . i x [ : , 0 , 0 , ' A ' ] L a b e l 1 1 . 4 9 5 3 0 9 L a b e l 2 0 . 7 3 9 7 7 6 N a m e :A ,d t y p e :f l o a t 6 4

Transposing
APanel4Dcanberearrangedusingitst r a n s p o s e method(whichdoesnotmakeacopybydefault unlessthedataareheterogeneous):

I n[ 1 3 8 ] :p 4 d . t r a n s p o s e ( 3 ,2 ,1 ,0 ) < c l a s s' p a n d a s . c o r e . p a n e l n d . P a n e l 4 D ' > D i m e n s i o n s :4( l a b e l s )x5( i t e m s )x2( m a j o r _ a x i s )x2( m i n o r _ a x i s ) L a b e l sa x i s :At oD


pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 26/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

I t e m sa x i s :2 0 0 0 0 1 0 10 0 : 0 0 : 0 0t o2 0 0 0 0 1 0 50 0 : 0 0 : 0 0 M a j o r _ a x i sa x i s :I t e m 1t oI t e m 2 M i n o r _ a x i sa x i s :L a b e l 1t oL a b e l 2

PanelND(Experimental)
PanelNDisamodulewithasetoffactoryfunctionstoenableausertoconstructNdimensional namedcontainerslikePanel4D,withacustomsetofaxislabels.Thusadomainspecificcontainer caneasilybecreated. ThefollowingcreatesaPanel5D.Anewpaneltypeobjectmustbesliceableintoalowerdimensional object.HereweslicetoaPanel4D.

I n[ 1 3 9 ] :f r o mp a n d a s . c o r ei m p o r tp a n e l n d

I n[ 1 4 0 ] :P a n e l 5 D=p a n e l n d . c r e a t e _ n d _ p a n e l _ f a c t o r y ( . . . . . : k l a s s _ n a m e =' P a n e l 5 D ' , . . . . . : a x i s _ o r d e r s =[' c o o l ' ,' l a b e l s ' , ' i t e m s ' , ' m a j o r _ a x i s ' , ' m i n o r _ a x i s ' ] , . . . . . : a x i s _ s l i c e s ={' l a b e l s ':' l a b e l s ' ,' i t e m s ':' i t e m s ' , . . . . . : ' m a j o r _ a x i s ':' m a j o r _ a x i s ' ,' m i n o r _ a x i s ':' m i n o r _ a x i s ' . . . . . : s l i c e r =P a n e l 4 D , . . . . . : a x i s _ a l i a s e s={' m a j o r ':' m a j o r _ a x i s ' ,' m i n o r ':' m i n o r _ a x i s '} , . . . . . : s t a t _ a x i s =2 ) . . . . . : I n[ 1 4 1 ] :p 5 d=P a n e l 5 D ( d i c t ( C 1=p 4 d ) ) I n[ 1 4 2 ] :p 5 d < c l a s s' p a n d a s . c o r e . p a n e l n d . P a n e l 5 D ' > D i m e n s i o n s :1( c o o l )x2( l a b e l s )x2( i t e m s )x5( m a j o r _ a x i s )x4( m i n o r _ a x i s ) C o o la x i s :C 1t oC 1 L a b e l sa x i s :L a b e l 1t oL a b e l 2 I t e m sa x i s :I t e m 1t oI t e m 2 M a j o r _ a x i sa x i s :2 0 0 0 0 1 0 10 0 : 0 0 : 0 0t o2 0 0 0 0 1 0 50 0 : 0 0 : 0 0 M i n o r _ a x i sa x i s :At oD #p r i n tas l i c eo fo u r5 D I n[ 1 4 3 ] :p 5 d . i x [ ' C 1 ' , : , : , 0 : 3 , : ] < c l a s s' p a n d a s . c o r e . p a n e l n d . P a n e l 4 D ' > D i m e n s i o n s :2( l a b e l s )x2( i t e m s )x3( m a j o r _ a x i s )x4( m i n o r _ a x i s ) L a b e l sa x i s :L a b e l 1t oL a b e l 2 I t e m sa x i s :I t e m 1t oI t e m 2 M a j o r _ a x i sa x i s :2 0 0 0 0 1 0 10 0 : 0 0 : 0 0t o2 0 0 0 0 1 0 30 0 : 0 0 : 0 0 M i n o r _ a x i sa x i s :At oD #t r a n s p o s ei t I n[ 1 4 4 ] :p 5 d . t r a n s p o s e ( 1 , 2 , 3 , 4 , 0 ) < c l a s s' p a n d a s . c o r e . p a n e l n d . P a n e l 5 D ' > D i m e n s i o n s :2( c o o l )x2( l a b e l s )x5( i t e m s )x4( m a j o r _ a x i s )x1( m i n o r _ a x i s ) C o o la x i s :L a b e l 1t oL a b e l 2 L a b e l sa x i s :I t e m 1t oI t e m 2 I t e m sa x i s :2 0 0 0 0 1 0 10 0 : 0 0 : 0 0t o2 0 0 0 0 1 0 50 0 : 0 0 : 0 0
pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe 27/28

8/28/13

Intro to Data Structures pandas 0.12.0 documentation

M a j o r _ a x i sa x i s :At oD M i n o r _ a x i sa x i s :C 1t oC 1 #l o o ka tt h es h a p e&d i m I n[ 1 4 5 ] :p 5 d . s h a p e ( 1 ,2 ,2 ,5 ,4 ) I n[ 1 4 6 ] :p 5 d . n d i m 5

pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe

28/28

S-ar putea să vă placă și