标签:
Series是一种类似一维数组的数据结构,输出时会同时显示索引和值
In [6]: a = Series([3,5,2])
In [7]: a
Out[7]:
0    3
1    5
2    2
dtype: int64分别得到索引和值
In [10]: a.values
Out[10]: array([3, 5, 2], dtype=int64)
In [11]: a.index
Out[11]: RangeIndex(start=0, stop=3, step=1)指定索引值
In [17]: b = Series([3,5,2],index = [‘a‘,‘b‘,‘c‘])
In [18]: b
Out[18]:
a    3
b    5
c    2
dtype: int64判断某key是否在字典中
In [22]: ‘a‘ in b
Out[22]: True
In [23]: ‘f‘ in b
Out[23]: False使用python字典创建Series
In [25]: a = Series({‘a‘:3, ‘b‘:6, ‘c‘:4})
In [26]: a
Out[26]:
a    3
b    6
c    4
dtype: int64创建时传入字典和索引序列,会自动把索引和字典中的索引对应起来,如果某索引不在字典中,其值为NaN
In [27]: dr = {‘a‘:3, ‘b‘:6, ‘c‘:4}
In [28]: idx = [‘a‘,‘b‘,‘c‘,‘d‘]
In [29]: b =  Series(dr,index = idx)
In [30]: b
Out[30]:
a    3.0
b    6.0
c    4.0
d    NaN
dtype: float64Series中元素的索引可以重复
In [31]: idx = [‘a‘,‘a‘]
In [32]: b = Series(dr,idx)
In [36]: b[‘a‘]
Out[36]:
a    3
a    3
dtype: int64判断各元素值是否为null
In [49]: b
Out[49]:
a    3.0
b    6.0
c    4.0
d    NaN
dtype: float64
In [50]: pd.isnull(b)
Out[50]:
a    False
b    False
c    False
d     True
dtype: bool
In [51]: pd.notnull(b)
Out[51]:
a     True
b     True
c     True
d    False
dtype: boolSeries在相互运算时会自动按照索引对应,NaN和任何数运算结果都是NaN
In [59]: t1
Out[59]:
a    3.0
d    NaN
dtype: float64
In [60]: t2
Out[60]:
a    2
d    3
dtype: int64
In [61]: t1 + t2
Out[61]:
a    5.0
d    NaN
dtype: float64可以给Series及它的索引命名
In [64]: b.name = ‘testname‘
In [65]: b.index.name = ‘indexname‘
In [70]: b
Out[70]:
indexname
a    3.0
b    6.0
c    4.0
d    NaN
Name: testname, dtype: float64index可以用直接赋值的方式修改
In [15]: b.index = [‘x‘,‘y‘,‘z‘,‘t‘]
In [16]: b
Out[16]:
x    2.0
y    3.0
z    4.0
t    NaN
dtype: float64DataFrame是一个表格形式的数据结构
可以通过传入一个字典来创建,字典的每个key是该维度的名字。传入后按列排在一起,会自动给每行加上0开始的索引。
In [18]: data
Out[18]: {‘a‘: [1, 2, 3], ‘b‘: [2, 4, 3], ‘c‘: [3, 5, 4]}
In [20]: frm = DataFrame(data)
In [21]: frm
Out[21]:
   a  b  c
0  1  2  3
1  2  4  5
2  3  3  4参数columns可以将列按指定顺序排列,如果列名称不在data中,则该列值均为NaN,index给每一行指定索引名
In [24]: frm = DataFrame(data,columns = [‘c‘,‘b‘,‘a‘],index = [‘one‘,‘two‘,‘three‘])
In [25]: frm
Out[25]:
       c  b  a
one    3  2  1
two    5  4  2
three  4  3  3访问某一行和列,返回Series。注意这是直接返回DataFrame的视图,如果要返回副本使用copy
In [26]: frm[‘c‘]
Out[26]:
one      3
two      5
three    4
Name: c, dtype: int64
In [28]: frm.ix[‘one‘]
Out[28]:
c    3
b    2
a    1
Name: one, dtype: int64使用Series对某一列赋值,会自动按照index匹配。在该Series中未出现的索引,不会出现在DataFrame中;在DataFrame中未出现的索引会被赋为NaN。
In [6]: frm = DataFrame(dr,index = [‘one‘,‘two‘,‘three‘])
In [8]: frm
Out[8]:
       a  b  c
one    3  2  1
two    2  4  4
three  1  1  2
In [9]: sr = Series(range(3),index = [‘two‘,‘one‘,‘three‘])
In [10]: frm[‘a‘] = sr
In [11]: frm
Out[11]:
       a  b  c
one    1  2  1
two    0  4  4
three  2  1  2通过直接赋值的方式创建新列
In [20]: frm
Out[20]:
         a  b  c
one    1.0  2  1
two    0.0  4  4
three  NaN  1  2
In [21]: frm[‘d‘] = 1
In [22]: frm
Out[22]:
         a  b  c  d
one    1.0  2  1  1
two    0.0  4  4  1
three  NaN  1  2  1使用2维字典创建DataFrame。第一维索引是每列的名称,第二维索引是每行的名称。
In [23]: dr2 = {‘a‘:{‘one‘:1,‘two‘:2},‘b‘:{‘one‘:3,‘three‘:4}}
In [24]: frm = DataFrame(dr2)
In [25]: frm
Out[25]:
         a    b
one    1.0  3.0
three  NaN  4.0
two    2.0  NaNDataFrame的values是一个Numpy的2维array
index是一种特殊的对象。该对象生成后不可修改。index也相当于一个集合,有求交,并等操作。
索引重排,使用reindex,对于原Series中不存在的索引,其值为fill_value(默认为NaN)
In [27]: sr = Series([1,2,3],index = [‘a‘,‘b‘,‘c‘])
In [28]: sr
Out[28]:
a    1
b    2
c    3
dtype: int64
In [30]: sr.reindex([‘c‘,‘a‘,‘b‘,‘d‘],fill_value = 0)
Out[30]:
c    3.0
a    1.0
b    2.0
d    0
dtype: float64按规则自动插值。下面的例子是前向插值(ffill),把每个未赋值的索引赋为前面一个元素的值。类似的方法还有bfill。
In [35]: sr2 = Series([0,1],index = [0,3])
In [36]: sr2
Out[36]:
0    0
3    1
dtype: int64
In [41]: sr2.reindex(range(5),method = ‘ffill‘)
Out[41]:
0    0
1    0
2    0
3    1
4    1
dtype: int64对DataFrame的行和列都可以进行reindex,但是只能在列上进行ffill等操作。 
使用ix也可以方便的对行进行reindex
使用drop方法,删除某一行或列,返回的是副本,不会影响原DataFrame。删除列需要指定axis = 1,因为axis默认为0
In [3]:  frm = DataFrame(np.arange(9).reshape(3,3),index = [‘a‘,‘b‘,‘c‘],columns = range(3))
In [4]: frm
Out[4]:
   0  1  2
a  0  1  2
b  3  4  5
c  6  7  8
In [7]: frm.drop(‘a‘)
Out[7]:
   0  1  2
b  3  4  5
c  6  7  8
In [6]: frm.drop(1,axis = 1)
Out[6]:
   0  2
a  0  2
b  3  5
c  6  8使用一个bool型Series做为Series的索引,会显示值为True那些索引。
In [8]: sr1 = Series(range(4),index = [‘a‘,‘b‘,‘c‘,‘d‘])
In [9]: sr1
Out[9]:
a    0
b    1
c    2
d    3
dtype: int64
In [13]: t = sr1>1
In [14]: t
Out[14]:
a    False
b    False
c     True
d     True
dtype: bool
In [16]: sr1[t]
Out[16]:
c    2
d    3
dtype: int64Series的切片运算左右都是闭区间
In [18]: sr1[‘a‘:‘d‘]
Out[18]:
a    0
b    1
c    2
d    3
dtype: int64DataFrame的索引比较复杂,主要分几种情况:
In [3]: frm = DataFrame(np.arange(9).reshape(3,3),index = [‘a‘,‘b‘,‘c‘],columns = range(3))
In [4]: frm
Out[4]:
   0  1  2
a  0  1  2
b  3  4  5
c  6  7  8
#直接用一个数索引,返回对应列
In [5]: frm[1]
Out[5]:
a    1
b    4
c    7
Name: 1, dtype: int32
#使用ix索引,返回对应行
In [6]: frm.ix[1]
Out[6]:
0    3
1    4
2    5
Name: b, dtype: int32
#使用区间索引,返回对应行,注意前两个返回的是Series,这个返回的是DataFrame
In [7]: frm[1:2]
Out[7]:
   0  1  2
b  3  4  5Series,DataFrame在相互运算时会自动按标签对齐运算,对于非交集标签运算结果为NaN,这个可以由fill_value指定
标签:
原文地址:http://blog.csdn.net/baoli1008/article/details/51356687