Pandas學習:DataFrame
本文介紹一些DataFrame的基本用法,由於只介紹少部分常見的,沒什麼難度,所以不做詳細的講解,直接上程式碼。
程式碼:
#coding=utf-8 ''' Created on 2017-2-20 @author: admin ''' import numpy as np from pandas import DataFrame from pandas import Series data={'state':['ohio','ohio','ohio','nevada','nevada'], 'year':[2000,2001,2003,2003,3004], 'pop':[1.1,1.2,1.3,1.4,1.5]} fram=DataFrame(data,columns=['year','state','debt'],index=['one','two','three','four','five']) print fram print 'year這一列所有資料:' print fram['year'] print '第n行資料:' print fram.ix['one'] print '對datafram中的值進行賦值:' fram['debt']=Series([1,2,3],index=['two','three','four']) print fram print '將frame中進行轉置:' print fram.T print '取frame中的值:' print fram.values
實驗結果:
year state debt
one 2000 ohio NaN
two 2001 ohio NaN
three 2003 ohio NaN
four 2003 nevada NaN
five 3004 nevada NaN
year這一列所有資料:
one 2000
two 2001
three 2003
four 2003
five 3004
Name: year, dtype: int64
第n行資料:
year 2000
state ohio
debt NaN
Name: one, dtype: object
對datafram中的值進行賦值:
year state debt
one 2000 ohio NaN
two 2001 ohio 1
three 2003 ohio 2
four 2003 nevada 3
five 3004 nevada NaN
將frame中進行轉置:
one two three four five
year 2000 2001 2003 2003 3004
state ohio ohio ohio nevada nevada
debt NaN 1 2 3 NaN
取frame中的值:
[[2000L 'ohio' nan]
[2001L 'ohio' 1.0]
[2003L 'ohio' 2.0]
[2003L 'nevada' 3.0]
[3004L 'nevada' nan]]
這裡對於y用Series和DataFrame進行資料操作等步驟留到下文進行詳細描述和實驗
如有錯誤歡迎批評指出。