Friday, October 23, 2015

Python: Pandas (1) index data frame

Abstract: index data frame by colname, rowname, or index

The script:
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 14 22:48:48 2015

@author: yuan
"""

import pandas as pd
import numpy as np

#Data frame
if __name__=="__main__":
    #creat a data frame
    df = pd.DataFrame(np.random.rand(3,4), columns=list("ABCD"), index=list("abc"))
    print 'Creat a data frame:\n', df

       
    #Name of columns and rows
    print 'Get column names:', list(df)
    print 'Get row names:', list(df.index)
    #change row names
    df.index=['aa','bb','cc']
    print 'assign new row names', list(df.index)
    #change column names
    df.columns=['a','b','c','d']
    #df.columns=list('abcd') also works
    print 'assign new columns names', list(df)

    #change a specific column or row name
    df=df.rename(columns={'b':'bb'})
    print 'Change a specific column name:', list(df)
    df=df.rename(index={'bb':'b3'})
    print 'Change a specific row name:', list(df.index)
   
    #index by columns using different methods
    print '\n', df
    #Note: return is Series structure
    print 'The first column by name:\n', df['a']
    print 'The first column by index:\n', df.ix[:,0]   
    print 'The first two columns by name:\n', df[['a','bb']]
    print 'The first two columns by index:\n', df.ix[:,0:2]
    print 'The first row by index:\n', df.ix[0]
    print 'The first row by name:\n', df.ix['aa']
    print 'The first two rows by index:\n', df.ix[0:2]
    print 'The first two rows by name:\n', df.ix[['aa','b3']]
   
    print 'The last column:', df.ix[:,-1]
    print 'The last row:', df.ix[-1]
    #

   
    print 'ok'

No comments:

Post a Comment