Anda di halaman 1dari 3

Series Notes

July 2, 2017

In [2]: import pandas as pd

In [54]: """ s = pd.Series(data, index=index) """


""" Can contain any data type"""
pd.Series(["a","b","c"], ["A", "B","C"])+"a"

Out[54]: A aa
B ba
C ca
dtype: object

In [55]: pd.Series(np.random.randn(5)) # default label, staring w/ 0

Out[55]: 0 -0.602030
1 -1.042223
2 -0.132997
3 -0.601916
4 -0.056615
dtype: float64

In [17]: s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])


s

Out[17]: a 0.348499
b -0.171071
c 1.649969
d -1.276476
e -0.303021
dtype: float64

In [11]: d = {'a' : 0., 'b' : 1., 'c' : 2.}


pd.Series(d)

Out[11]: a 0.0
b 1.0
c 2.0
dtype: float64

In [12]: pd.Series(d, index=['b', 'c', 'd', 'a']) # label shows the order or arrang

1
Out[12]: b 1.0
c 2.0
d NaN
a 0.0
dtype: float64

In [13]: pd.Series(5., index=['a', 'b', 'c', 'd', 'e']) # equal value

Out[13]: a 5.0
b 5.0
c 5.0
d 5.0
e 5.0
dtype: float64

In [21]: """ Series is ndarray-like """


s[0]

Out[21]: 0.3484993615997764

In [27]: s[:3] # first 3, i.e. 0,1,2

Out[27]: a 0.348499
b -0.171071
c 1.649969
dtype: float64

In [23]: s[s > s.median()] # since [] returns True/False

Out[23]: a 0.348499
c 1.649969
dtype: float64

In [24]: s[[4, 3, 1]]

Out[24]: e -0.303021
d -1.276476
b -0.171071
dtype: float64

In [28]: np.exp(s) # label does not change, only values get exp'ed

Out[28]: a 1.416940
b 0.842761
c 5.206817
d 0.279019
e 0.738584
dtype: float64

In [44]: s + s

2
Out[44]: a 0.696999
b -0.342143
c 3.299937
d -2.552952
e -0.606042
dtype: float64

In [45]: (s+s) == s*2

Out[45]: a True
b True
c True
d True
e True
dtype: bool

In [37]: """ Series is dict like """


s['a']
s['e']

Out[37]: -0.30302080406783488

In [38]: 'e' in s # label in s

Out[38]: True

In [40]: -0.30302080406783488 in s

Out[40]: False

In [43]: s.get('e') == s['e']

Out[43]: True

In [50]: s[2:] # remove the first 2

Out[50]: c 1.649969
d -1.276476
e -0.303021
dtype: float64

In [51]: s[:-2] # remove the last 2

Out[51]: a 0.348499
b -0.171071
c 1.649969
dtype: float64

In [53]: s.index

Out[53]: Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

In [ ]:

Anda mungkin juga menyukai