Introduction to Series Data Structure in Pandas Library

Series 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.
In[2]: import pandas as pd      # Import Pandas library with pd as interface for using it
       import numpy as np       # Import NumPy library with np as interface for using it
       arr = np.array(['Shahrukh', 'Salman', 'Amir', 'Akshay']) # Declare an array
       a_series= pd.Series(arr) # Create a Series from array data
       print(a_series)          # Print created Series
Running the above lines of code, you will get following output as created Series:
0    Shahrukh
1      Salman
2        Amir
3      Akshay
dtype: object
From the above result, you can notice that we have not assigned axis labels as a parameter to Series constructor, so pandas library by default automatically assigned index label with integer values ranging from 0 to N-1 (where N is the total number of elements in data).
Now 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 Series
You will get Series  shown below by running above lines of code;
3    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 Series
You will get following result as Series of above lines
a    5
b    3
c    1
d    4
dtype: int64
From the above resulted Series, you will note that index of Series will be automatically assigned equal to Keys of Dict object mentioned as data in Series function.
We 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 Series
The output of above code will be as follows. Here we changed the order of index labels and subsequently, the order of items will also change.
c    1
a    5
d    4
b    3
dtype: int64
Let's eloborate another example below with index lables list assigned;
In[7]:  d_series=pd.Series(d, index=['c', 'a', 'e', 'b' ])
        print(d_series)   # Print a Series
You will be shown following output of above lines of code and you will notice that index label is assigning to NaN because there is no any value with key 'e' in dictionary object d.
c    1.0
a    5.0
e    NaN
b    3.0
dtype: float64
In this way, you can create a Series with nd-array, dict and scalar data using the examples discussed above. In the next post, we will talk about more on Accessing and Retreiving from a Series Data Strucure

Comments

  1. Thanks for the post. Is there a way I can set up an alert for the new blog posts ?

    ReplyDelete
    Replies
    1. Thanks 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

Post a Comment