The following blog will let you know about the one of the Data Structure of Pandas Library i.e. Series. The blog will make you understand how to create Series and what are different ways to access the data from Series.
Series
Series is one of the data structure of Pandas library. It is basically a one-dimensional array which can store data of any type like string , object, integer etc. The syntax of creating a series object is show below.
pandas.Series(data,index,dtype,copy)
The Series constructor takes four arguments i.e. data, index, dtype, copy. You might have noticed one thing that i have used pandas keyword in the above syntax. It is nothing but it as the package which we will be using in order to create or use series as you will some examples below. Now, Let me explain you in brief about the arguments used above.
-
data : data is basically the content which you want to store in series. It can be anything like array , dictionary or any scalar value (constant value).
-
index : index will represent the indexes of the data you passed. If you have not given the index then by default the python range(len(data)) will be used internally to create the index. Please do ensure one thing that index values must have the same length as the data. If you have used a dictionary and index both simultaneously then the your dictionary keys will be overriden by the index value. I will show you later how it will happen with help of a suitable example.
-
dtype : dtype basically shows the data type of the data you have passed. Again It is an optional parameter. If you have not mention the dtype explicitly then the pandas will automatically assign the dtype value as per your data passed.
-
copy : Another optional parameter which is used to make the copy of the data you have passed. It takes boolean value i.e. True or False. Its default value is set to be False.
Note
-
The term dictionary used above is nothing but it is just like a map in Java i.e. key-value pair.
-
The range function is used to create the list of numbers based upon the size passed in its argument. It is often used to iterate over the for loops.
Implementation
-
Create a Series
You have to import the pandas package and you can give the aliasing as i have given pan as shown above. Now, simply use the Series function and pass the desired values. Print the series object and see the result as shown below. The 1,2,3,4 and 5 are the values you have passed and the 0,1,2,3,4 are indexes corresponding to that values.
-
Create a Series from dictionary
Again import the pandas package and create the dictionary object. Thereafter simply use the Series function and pass the dictionary object as argument as shown above. Print the series object and you will notice that the keys in dictionary will become your indexes if you have not given the indexes value explicitly as shown below.
-
Create a Series using dictionary with indexes
Import the required package i.e pandas. create the dictionary object. Now, simply use the Series function and pass the dictionary object along with index values as shown above. Print the series object and notice the change in index values. In case of dictionary, always put the index values same as your dictionary keys else the keys will be overridden and you will not get the desired value. Instead of the desired value you will get NaN(Not a Number)
Note
If you have passed the index values more than the number of key-value pairs then the NaN(Not a Number) will be added as a value for that particular index as shown below.
Hopefully, you got an idea that how to create Series using pandas. I will be covering the DataFrame Data Structure of pandas in next blog till then keep reading Pandas. Please feel free to share your valuable feedback and ask any kind of questions.
Thank You for Reading