






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







- May 12, Shiu-Tang Li 1 p r i n t ( d a t a f r a m e. head ( 5 ) ) 2 # p r i n t f i r s t 5 rows 3 p r i n t ( d a t a f r a m e ) 4 # p r i n t t h e data frame , dimension i n f o r m a t i o n i s a l s o a t t a c h e d
1 p r i n t ( d a t a f r a m e. d e s c r i b e ( ) )
1 dim = d a t a f r a m e. shape 2 number of rows = dim [0] 3 number of columns = dim [1]
1 t y p e s = d a t a f r a m e. d t y p e s 2 # t y p e s i s a S e r i e s l a b e l e d by column names , showing th e data t y p e f o r each column. 3 i n t e g e r i n d e x = t y p e s [ t y p e s == i n t 6 4 ]. index 4 # t y p e s [ t y p e s == i n t 6 4 ] i s a f i l t e r e d S e r i e s with i n t e g e r v a l u e s. 5 p r i n t ( d a t a f r a m e [ i n t e g e r i n d e x ] ) 6 # t h i s g i v e s us t h e f i l t e r e d data frame with o n l y i n t 6 4 v a l u e s.
1 p r i n t ( d a t a f r a m e [ ’ column name ’ ]. unique ( ) ) 2 # w i l l r e t u r n a l i s t showing d i s t i n c t elements i n th e column 3 p r i n t ( d a t a f r a m e [ ’ column name ’ ]. v a l u e c o u n t s ( ) ) 4 # w i l l r e t u r n a t a b l e showing t h e c o u n t s i n th e column
1 p r i n t ( d a t a f r a m e. columns ) 2 # d a t a f r a m e. columns i s a l i s t o f s t r i n g s 3 f i r s t c o l u m n = d a t a f r a m e. columns [0] 4 # p r i n t t h e f i s t column , which i s a s t r i n g
1 column = d a t a f r a m e [ ’ column name ’ ] 2 # column i s a [ S e r i e s ] o b j e c t , c o n t a i n s row index + v a l u e s , both a r e l i s t s 3 c o l u m n v a l u e s = column. v a l u e s 4 column index = column. index
1 d a t a f r a m e 2 = d a t a f r a m e [ [ ’ column name1 ’ , ’ column name2 ’ ] ] 2 d a t a f r a m e 2 = d a t a f r a m e [ d a t a f r a m e. column [ 0 : 2 ] ] 3 # s e l e c t t h e f i r s t two columns i n two d i f f e r e n t ways 4 d a t a f r a m e 2 = d a t a f r a m e [ ’ column name x ’ : ’ column name y ’ ] 5 # s e l e c t t h e columns between t h e two columns
1 d a t a f r a m e [ d a t a f r a m e [ ’ column name ’ ] == some values ]
1 d a t a f r a m e. i l o c [ i ] 2 #i : row index 3 d a t a f r a m e. i l o c [ 0 : 3 ] 4 # s e l e c t t h e rows with i n d i c e s 0 ,1 ,
1 d a t a f r a m e [ ’ column name ’ ]. idxmax ( ) 2 # r e t u r n s t h e 1 s t row index t h a t has max
1 # i : t r u e row index 2 # Approach 1 ( w i l l g e t warning message ) : 3 d a t a f r a m e. i x [ i , ’ column name ’ ] = new value 4 # Approach 2 ( w i l l g e t warning message ) : 5 d a t a f r a m e [ ’ column name ’ ] [ i ] = new value 6 # Approach 3 : 7 d a t a f r a m e. s e t v a l u e ( i , ’ column name ’ , new value ) 8 # Approach 4 : 9 d a t a f r a m e. a t [ i , ’ column name ’ ] = new value
1 d a t a f r a m e. index = [ index1 , index2 ,... ] 2 # r e p l a c e i n d i c e s with a new l i s t 3 d a t a f r a m e = d a t a f r a m e. s e t i n d e x ( [ ’ column name ’ ] ) 4 # indexed by a p a r t i c u l a r column 5 d a t a f r a m e = d a t a f r a m e. r e s e t i n d e x ( drop=True ) 6 # r e i n d e x e d from 0. drop=F a l s e : make a dataframe column with th e o l d index v a l u e s.
1 o l d i n d i c e s = d a t a f r a m e. index 2 n e w i n d i c e s = o l d i n d i c e s. v a l u e s 3 f o r i , item i n enumerate ( n e w i n d i c e s ) : 4 i f item == ’ o l d i n d e x t o b e c h a n g e d ’ : 5 n e w i n d i c e s [ i ] = ’ new index ’ 6 d a t a f r a m e. index = n e w i n d i c e s
1 d a t a f r a m e. rename ( columns={ ’ column name1 ’ : ’ new column name1 ’ , ’ column name2 ’ : ’ new column name2 ’ } , i n p l a c e=True ) 2 # rename a few columns : w i l l g e t warning message 3 d a t a f r a m e. columns = [ ’ column name1 ’ , ’ column name2 ’ ,... ] 4 # rename a l l columns
1 d a t a f r a m e. drop ( ’ column name ’ , a x i s =1, i n p l a c e=True ) 2 # drop a column 3 d a t a f r a m e. drop ( [ ’ column name1 ’ , ’ column name2 ’ ,... ] , a x i s =1, i n p l a c e=True ) 4 # drop a few columns 5 d a t a f r a m e. drop ( ’ row index1 ’ , a x i s =0, i n p l a c e=True ) 6 # drop a row
7 d a t a f r a m e. drop ( [ ’ row index1 ’ , ’ row index2 ’ ,... ] , a x i s =0, i n p l a c e=True ) 8 # drop a few rows
1 import pandas as pd 2 i s n u l l = pd. i s n u l l ( d a t a f r a m e [ ” column name ” ] ) 3 # w i l l r e t u r n a t r u e / f a l s e S e r i e s. N u l l v a l u e = NaN. 4 new data frame = d a t a f r a m e. dropna ( ) 5 # drop a l l rows with m i s s i n g v a l u e s 6 new data frame = d a t a f r a m e. dropna ( a x i s = 1) 7 # drop a l l columns with m i s s i n g v a l u e s 8 new data frame = d a t a f r a m e. dropna ( s u b s e t =[” column1 ” , ” column2 ” ] ) 9 # drop a l l rows with m i s s i n g v a l u e s i n t h e two columns 10 d a t a f r a m e [ ” column ” ] = d a t a f r a m e [ ” column ” ]. f i l l n a ( d a t a f r a m e [ ” column ” ]. median ( ) ) 11 d a t a f r a m e [ ” column ” ] = d a t a f r a m e [ ” column ” ]. f i l l n a ( d a t a f r a m e [ ” column ” ]. mean ( ) ) 12 d a t a f r a m e [ ” column ” ] = d a t a f r a m e [ ” column ” ]. f i l l n a ( something ) 13 # f i l l NaN v a l u e s with column median / mean / or s t h e l s e
1 d a t a f r a m e. T
1 d a t a f r a m e [ ’ column name ’ ] = d a t a f r a m e [ ’ column name ’ ]. a s t y p e ( f l o a t ) 2 # changing t y p e s t o f l o a t
1 import pandas as pd 2 l i s t 1 = [{ ’ c1 ’ : 4 , ’ c2 ’ : 3 } , { ’ c1 ’ : 2 , ’ c2 ’ : 3 }] 3 l i s t 2 = [{ ’ c1 ’ : 6 , ’ c2 ’ : 9 } , { ’ c1 ’ : 8 , ’ c2 ’ : 1 0 } ] 4 df1 = pd. DataFrame ( l i s t 1 , index =[0 ,1]) 5 df2 = pd. DataFrame ( l i s t 2 , index=[ ’ two ’ , ’ t h r e e ’ ] ) 6 d f = pd. c o n c a t ( [ df1 , df2 ] ) 7 # combine two data frames with common columns , i n c r e a s e rows. index could c o n t a i n keys o f d i f f e r e n t data t y p e s
1 import pandas as pd 2 df1 = pd. DataFrame ({ ’ key ’ : [ ’ a ’ , ’ b ’ ] , ’ c1 ’ : [ 1 , 2 ] , ’ c2 ’ : [ 8 , 2]}) 3 df2 = pd. DataFrame ({ ’ key ’ : [ ’ a ’ , ’ b ’ ] , ’ c3 ’ : [ 3 , 5]}) 4 merged df = pd. merge ( df1 , df2 , on= ’ key ’ ) 5 # combine two data frames with t h e same data i n th e column ’ key ’. Merged data frame w i l l c o n t a i n 4 columns.
1 d a t a f r a m e. s o r t ( ” column name ” , i n p l a c e=True , a s c e n d i n g=F a l s e ) 2 # s o r t by column , i n d e c r e a s i n g o r d e r 3 d a t a f r a m e. s o r t i n d e x ( i n p l a c e=True , a s c e n d i n g=F a l s e ) 4 # s o r t by index , i n d e c r e a s i n g o r d e r
1 import numpy as np 2 new data frame = d a t a f r a m e. p i v o t t a b l e ( index=” column1 ” , v a l u e s=” column2 ” , aggfunc=np. mean) 3 # For d i f f e r e n t column1 v a l u e s , f i n d t h e r e s u l t o f a g g f u n c t i o n a p p l i e d t o column. 4 new data frame = d a t a f r a m e. p i v o t t a b l e ( index=” column1 ” , v a l u e s=” any column ” , aggfunc= ’ count ’ ) 5 # count t h e f r e q u e n c y f o r column.
1 groups = d a t a f r a m e. groupby ( ” column1 ” ) 2 t a b l e = groups. a g g r e g a t e ( np. mean) [ ” column2 ” ] 3 # c l a s s i f y rows i n t o d i f f e r e n t groups based on ’ column1 ’ , f o r each group a ppl y np. mean ( ) on a l l v a l u e s i n ” column2 ”.
1 apply1 = d a t a f r a m e. a p p l y ( s o m e f u n c t i o n ) OR 2 apply1 = d a t a f r a m e. a p p l y ( lambda x : s o m e f u n c t i o n ( x ) ) 3 # a p p l y f u n c t i o n t o columns , t o g e t a S e r e i s o b j e c t l a b e l e d column names 4 apply2 = d a t a f r a m e. a p p l y ( s o m e f u nc t i o n , a x i s =1) OR 5 apply2 = d a t a f r a m e. a p p l y ( lambda x : s o m e f u n c t i o n ( x ) , a x i s =1) 6 # a p p l y f u n c t i o n t o rows , t o g e t a S e r e i s o b j e c t l a b e l e d row i n d i c e s