本文介绍一些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进行数据操作等步骤留到下文进行详细描述和实验
如有错误欢迎批评指出。