data : array like , iterable , dictionary, list, tuple , scalar index : have the same length as data, Not necessarily unique,must be a hashable type dtype : Data type of output series name: Name given to series copy: Default is false, Copy input data
Using head() and tail() ( above code with 35 records to be used )
print(s.head()) # First five records
print(s.head(2)) # last five records
print(s.tail()) # last five records
print(s.tail(2)) # last two records
print(s[:5]) # first 5 records
my_dict={'a':'Ravi','b':'Raju','c':'Alex'}
s=pd.Series(data=my_dict)
s['b']='King' # updating using index
print(s)
Output
a Ravi
b King
c Alex
Converting Data type of Series
By default this will be inferred from data. Here the id column is integer so by default the dtype is integer (dtype: int64) . Here we have specified the dtype to be string. Check the output by removing dtype='string'. How to create sample student DataFrame?
df= pd.read_csv('D:\\my_data\\student.csv') # csv file
s=pd.Series(df['id'],dtype='string')
print(s) # Name : id , dtype string
Use this sample series for all examples below.
import pandas as pd
l1=[52,13,45,39] # List of values
#l1=['One','Tow','Three','Four'] # List of values
my_id=['x','b','y','p'] # List of index
s=pd.Series(l1,index=my_id) # create a series
Apply function to increase the value of all elements
s=s.apply(lambda x:x+5) # operation on each element
List of index of the series
print(s.index.tolist()) # List of index
Value based on index
print(s['y']) # Key error if not found
print(s.sum()) # sum of all values / elements
Find the maximum , minimum , median, mode of the series
print(s.min()) # use max(), mean(),median(),mode()
Sorting of values and index, use the options ascending, inplace, kind, na_position,ignore_index, Key
print(s.sort_values()) #
print(s.sort_index())
Delete one element using index. Watch the option inplace
s.drop(labels=['b'],inplace=True)
s.pop('x') # remove element using key
Adding element using index.
s['z']=50 # adding element using key
print(s.filter(items=['x','p'])) # searching based on key
Searching for values in a series.
print(s.isin([50,13])) # Matching True or False
#print(s[s.isin([50,13])]) # Matching row only
#print(s[s.isin([50,13])].tolist()) # Matching rows only
#print(s[s.isin([13,39])].index.tolist()) # Matching index or keys
to_numpy()
print(s.to_numpy()) # value to Numpy
print(s.index.to_numpy()) # index to Numpy
print(type(s.to_numpy())) # class 'numpy.ndarray'