10 23 56 17 52 61 73 90 26 72
seriesVar = pandas.Series( data, index, dtype, copy)
The parameters of the constructor are:
Default: [ ]
Default: np.arrange(n), i.e.: [0 .. (n-1)]
Default: data type is inferred from data
Default: False
#import the pandas library and aliasing as pd import pandas as pd s1 = pd.Series() print(s1) # Output: Series([], dtype: float64)
#import the pandas library and aliasing as pd import pandas as pd s2 = pd.Series([9, 8]) print(s2) Output: 0 9 (0, 1 are indexes) 1 8 dtype: int64
DEMO: progs/series01.py
data = ['a', b', 'c'] s1 = pd.Series(data,index=[100,101,99]) print(s1) Output: 100 a 101 b 99 c dtype: object
s2 = pd.Series(data, ['x', 'y', 'z']) print(s2) Output: x a y b z c dtype: object
Example: no index specified
data = {'a' : 0.0, 'b' : 1.0, 'c' : 2.0} s = pd.Series(data) print(s) Output: a 0.0 b 1.0 c 2.0 dtype: float64
Demo: progs/series03.py
Example: index is passed
data = {'a' : 0.0, 'b' : 1.0, 'c' : 2.0} s = pd.Series(data, ['c', 'b', 'a', 'd']) print(s) Output: c 2.0 b 1.0 a 0.0 d NaN
seriesVar[ k ]
seriesVar[ index ]
Example:
s1 = pd.Series([4,5,6,7,8], index = ['a','b','c','d','e']) print(s1[0]) ---> 4 print(s1[4]) ---> 8 print(s1['a']) ---> 4 print(s1['e']) ---> 8
Comment: a series is similar to a Python dict... but can also use positional "index"
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) print(s[1:3]) b 5 c 6 print(s[1:]) b 5 c 6 d 7 e 8 print(s[:3]) a 4 b 5 c 6
DEMO: progs/series05.py
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) print(s[['a','c','d']]) Output: a 4 c 6 d 7