具体方法参见:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.html
Series 是一个一维数组结构的,可以存入任一一种python的数据类型(integers, strings, floating point numbers, Python objects, etc.)。
创建一个Series的最基本方法是:
>>> s = pd.Series(data, index=index)
程序:
# -*- coding: utf-8 -*-#-*-coding : utf-8 -*-
import numpy as np
import pandas as pd
s = pd.Series([1,2,3,np.nan,6]) #np.nan为NAN;
q =pd.Series({'a':3,'b':4,'c':5,'f':6,'e':8}) #以一字典结构为参数
print q
print "len():",len(s) #Series(s)的长度,包括NAN;注意: NAN在pandas中不是一个值是缺省值
print "shape():",np.shape(s) #矩阵形状;
print "count():",s.count() #Series的长度,不包括NAN;
print "unique():",s.unique() #出现不重复的values值;
print "value_counts():\n",s.value_counts() #同意values值得次数;
print s
print '*'*40
t = pd.Series(np.random.randn(5),index=['a','b','c','d','e'],name='something') # pd.Series([list],index=[list])//以list为参数,参数为一list;index为可选参数,
print t #若不填则默认index从0开始;若添则index长度与value长度相等
print '^'*40
m = pd.Series(5.,index=['a','b','c','d','e'])
print m
print '%'*40
a = pd.Series(np.random.random_sample(20))
print a
print a.head() #取出头n行,n为可选参数,默认n=5;
print a.tail() #取末尾n行,n为可选参数,默认n=5;
print '$'*40
print m+t #相同index的value相加,若index并非共有的则该index对应value变为NaN
print '@'*40
print s+t
结果:
a 3
b 4
c 5
e 8
f 6
dtype: int64
len(): 5
shape(): (5L,)
count(): 4
unique(): [ 1. 2. 3. nan 6.]
value_counts():
6.0 1
3.0 1
2.0 1
1.0 1
dtype: int64
0 1.0
1 2.0
2 3.0
3 NaN
4 6.0
dtype: float64
****************************************
a -0.927462
b -0.027922
c -1.569187
d -1.595163
e 0.547615
Name: something, dtype: float64
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
a 5.0
b 5.0
c 5.0
d 5.0
e 5.0
dtype: float64
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0 0.718951
1 0.826478
2 0.484672
3 0.792322
4 0.336493
5 0.764659
6 0.515082
7 0.917730
8 0.131164
9 0.358306
10 0.557721
11 0.286593
12 0.507390
13 0.877456
14 0.614322
15 0.985765
16 0.087540
17 0.336409
18 0.543998
19 0.319411
dtype: float64
0 0.718951
1 0.826478
2 0.484672
3 0.792322
4 0.336493
dtype: float64
15 0.985765
16 0.087540
17 0.336409
18 0.543998
19 0.319411
dtype: float64
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
a 4.072538
b 4.972078
c 3.430813
d 3.404837
e 5.547615
dtype: float64
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
a NaN
b NaN
c NaN
d NaN
e NaN
dtype: float64