Docsity

Python pandas quick guide, Schemes and Mind Maps of Web Programming and Technologies

Toggle sidebar
bg1
Python pandas quick guide
Shiu-Tang Li
May 12, 2016
Contents
1 Dataframe initialization / outputs 3
1.1 Load csv files into dataframe. .......................... 3
1.2 Initialize a dataframe ............................... 3
1.3 Create a new column ............................... 3
1.4 Output a dataframe to csv ............................ 3
2 Take a quick glance of a dataframe 4
2.1 Print the data frame ............................... 4
2.2 Get the description of numerical columns ................... 4
2.3 Get the dimension ................................ 4
2.4 Get the data type / get the filtered data by data type ............ 4
2.5 Get the unique elements ............................. 4
3 Select data from a dataframe 5
3.1 Get column names ................................ 5
3.2 Select a specific column ............................. 5
3.3 Select the sub-dataframe of a few columns ................... 5
3.4 Select rows with restrictions on columns .................... 5
3.5 Select rows with row index ........................... 5
3.6 Select row index with max values in a specific column ............ 5
3.7 Select given entry ................................. 5
3.8 Iterate rows .................................... 6
4 Revise data in a dataframe 7
4.1 Revise data in a particular entry ........................ 7
4.2 Reindex rows ................................... 7
4.3 Reindex one row ................................. 7
4.4 Rename columns ................................. 7
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Python pandas quick guide and more Schemes and Mind Maps Web Programming and Technologies in PDF only on Docsity!

Python pandas quick guide

 - May 12, Shiu-Tang Li 
  • 1 Dataframe initialization / outputs Contents
    • 1.1 Load csv files into dataframe.
    • 1.2 Initialize a dataframe
    • 1.3 Create a new column
    • 1.4 Output a dataframe to csv
  • 2 Take a quick glance of a dataframe
    • 2.1 Print the data frame
    • 2.2 Get the description of numerical columns
    • 2.3 Get the dimension
    • 2.4 Get the data type / get the filtered data by data type
    • 2.5 Get the unique elements
  • 3 Select data from a dataframe
    • 3.1 Get column names
    • 3.2 Select a specific column
    • 3.3 Select the sub-dataframe of a few columns
    • 3.4 Select rows with restrictions on columns
    • 3.5 Select rows with row index
    • 3.6 Select row index with max values in a specific column
    • 3.7 Select given entry
    • 3.8 Iterate rows
  • 4 Revise data in a dataframe
    • 4.1 Revise data in a particular entry
    • 4.2 Reindex rows
    • 4.3 Reindex one row
    • 4.4 Rename columns
    • 4.5 Drop columns / rows
    • 4.6 Find / drop / fill missing values
    • 4.7 Data frame transpose
    • 4.8 Change types of a column
    • 4.9 Merge data frames
  • 5 Search key words in a dataframe
    • 5.1 Exact match in target column
  • 6 Perform operations on a dataframe
    • 6.1 Sort dataframe
    • 6.2 Rearrange dataframe - pivot table
    • 6.3 Grouping
    • 6.4 ‘Apply’ function
  • 7 Others

2 Take a quick glance of a dataframe

2.1 Print the data frame

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

2.2 Get the description of numerical columns

1 p r i n t ( d a t a f r a m e. d e s c r i b e ( ) )

2.3 Get the dimension

1 dim = d a t a f r a m e. shape 2 number of rows = dim [0] 3 number of columns = dim [1]

2.4 Get the data type / get the filtered data by data type

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.

2.5 Get the unique elements

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

3 Select data from a dataframe

3.1 Get column names

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

3.2 Select a specific column

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

3.3 Select the sub-dataframe of a few columns

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

3.4 Select rows with restrictions on columns

1 d a t a f r a m e [ d a t a f r a m e [ ’ column name ’ ] == some values ]

3.5 Select rows with row index

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 ,

Remark. The difference between loc and iloc: If the index of the dataframe is 3, 7, 0, 2,

.. ., iloc[0] will select the third row (true integer index), loc will select the 1st row (index

by locations).

3.6 Select row index with max values in a specific column

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

3.7 Select given entry

4 Revise data in a dataframe

4.1 Revise data in a particular entry

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

4.2 Reindex rows

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.

4.3 Reindex one row

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

4.4 Rename columns

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

4.5 Drop columns / rows

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

4.6 Find / drop / fill missing values

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

4.7 Data frame transpose

1 d a t a f r a m e. T

4.8 Change types of a column

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

4.9 Merge data frames

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.

6 Perform operations on a dataframe

6.1 Sort dataframe

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

6.2 Rearrange dataframe - pivot table

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.

Remark. Other choices of aggfunc: ’max’, ’min’, np.std, np.sum, np.median.

6.3 Grouping

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 ”.

6.4 ‘Apply’ function

c1 c2 c3 apply

r1 - - - ap

r2 - - - ap

r3 - - - ap

apply1 ap1 ap1 ap

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

7 Others

Series.tolist() converts Series to lists.

numpy.nan: missing value