Exp8 SBLC
Exp8 SBLC
Date of Performance :
Date of Submission :
Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float,
python objects, etc.). The axis labels are collectively called index.
pandas.Series
● Array
● Dict
● Scalar value or constant
Create an Empty Series
If data is an ndarray, then index passed must be of the same length. If no index is passed, then by default
index will be range(n) where n is array length, i.e., [0,1,2,3…. range(len(array))-1].
Example
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data)
print s
We did not pass any index, so by default, it assigned the indexes ranging from 0 to len(data)-1, i.e., 0 to
3.
Example
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data,index=[100,101,102,103])
print s
We passed the index values here. Now we can see the customized indexed values in the output.
Create a Series from dict
A dict can be passed as input and if no index is specified, then the dictionary keys are taken in a sorted
order to construct index. If index is passed, the values in data corresponding to the labels in the index
will be pulled out.
Example
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data ={'a':0.,'b':1.,'c':2.}
s = pd.Series(data)
print s
Example
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data ={'a':0.,'b':1.,'c':2.}
s = pd.Series(data,index=['b','c','d','a'])
print s
Observe − Index order is persisted and the missing element is filled with NaN (Not a Number).
If data is a scalar value, an index must be provided. The value will be repeated to match the length of
index
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
s = pd.Series(5, index=[0,1,2,3])
print s
Retrieve the first element. As we already know, the counting starts from zero for the array, which means
the first element is stored at zeroth position and so on.
import pandas as pd
s = pd.Series([1,2,3,4,5],index =['a','b','c','d','e'])
Retrieve the first three elements in the Series. If a : is inserted in front of it, all items from that index
onwards will be extracted. If two parameters (with : between them) is used, items between the two
indexes (not including the stop index)
import pandas as pd
s = pd.Series([1,2,3,4,5],index =['a','b','c','d','e'])
A Series is like a fixed-size dict in that you can get and set values by index label.
Example
#retrieve a single
element print s['a']
#retrieve multiple
elements print
s[['a','c','d']]
#retrieve multiple
elements print s['f']
1. Get the minimum, 25th percentile, median, 75th, and max of a numeric series
1. Explore various useful methods of Data series
R1 R2 R3 R4 Total
Signature
(3 Marks) (3 Marks) (3 Marks) (1 Mark) (10Marks)