Introduction to Series Data Structure in Pandas Library
Series is one of the fundamental data structures of the Pandas Library which is built on top of NumPy Library. Series is actually is one dimensional labelled array object similar to list, dict or column of a table. Series object which can hold any data forms (i.e. Python dict, ndarray and scalar values e.g. 5) will assign labelled index to each item in its Series. And by default each item of a Series will be assigned index labels from 0 to (N-1), where N is total number of Series items.
How to create a Series data structure?
Using Pandas library, we can create Series using the following constructor highlighted below;
s = pandas.Series(data, index, dtype)
From the above line of code, we used parameters as arguments to Series functions defined below;
- data may be of any form (i.e. Python dict, ndarray and scalars) that we store it as contents or items of Series
- index is the list of unique axis labels equal to the length of data. If the index labels is not defined then by default list of integers is assigned as labels to data.
- dtype parameter is used for telling Series constructor about the data type will it create for Series items. By default data type will be assigned based on data items.
You can create empty Series by Series constructor with no any parameters passed to it as shown in following example;
In[1]:  import pandas as pd   # Import Pandas library with pd as interface for using it
        s = pd.Series()       # Create an empty Series
        print(s)              # Print a Series object
After running the above lines of code, you will get output as below;
Creating a Series with nd-array
You can create a Series from data of array with index of the same length assigned to it as shown below example.Running the above lines of code, you will get following output as created Series:In[2]: import pandas as pd # Import Pandas library with pd as interface for using itimport numpy as np # Import NumPy library with np as interface for using itarr = np.array(['Shahrukh', 'Salman', 'Amir', 'Akshay']) # Declare an arraya_series= pd.Series(arr) # Create a Series from array dataprint(a_series) # Print created Series
0    Shahrukh
1      Salman
2        Amir
3      Akshay
dtype: objectNow in the next example we will assign index labels by providing a list (of any data type) along with data;
In[3]:  ranking_bypolls = [3,1,2,4]  # Declare a list of ranking
        b_series = pd.Series(arr, index = ranking_bypolls) # Create a Series with data and a list of index labels
        print(b_series)              # Print Result Series3    Shahrukh
1      Salman
2        Amir
4      Akshay
dtype: object
Creating a Series with Scalar
You can create a Series from Scalar data as mentioned following example; Here in the example you will notice we have used a data parameter as single scalar value 6 and then we assigned index labels with list ind defined. Length of data  will be enlarged equal to length of index labels.In[4]:  ind=['a','b','c','d','e']       # Declare index list
        s_series=pd.Series(6,index=ind) # Create an Series using Scalar data
        print(s_series)                 # Print Series Created
Creating a Series with Dictionary Object
You can create a Series with Dictionary object by passing Dict to Series constructor as data parameter;
In[5]:  d = {'a': 5, 'b': 3, 'c': 1, 'd': 4} # Declare a Dictionary
        d_series=pd.Series(d)           # Create a Series
        print(d_series)                 # Print a Seriesa    5
b    3
c    1
d    4
dtype: int64We can modify sequence of index labels of Series by assigned index parameter to new index labels list as shown below;
In[6]:  d_series=pd.Series(d, index=['c', 'a', 'd', 'b' ])
        print(d_series)    # Print a Seriesc    1
a    5
d    4
b    3
dtype: int64In[7]:  d_series=pd.Series(d, index=['c', 'a', 'e', 'b' ])
        print(d_series)   # Print a Series
c    1.0
a    5.0
e    NaN
b    3.0
dtype: float64
Thanks for the post. Is there a way I can set up an alert for the new blog posts ?
ReplyDeleteThanks for liking post.At left side, there is option for subscription in order to get notifications about new posts. And also like our Facebook page shown in the site. Thanks again for liking this post.
Delete