In this article, we’ll understand the Series method of Python pandas. From the definition to the practical implementation of the series() method, everything will be discussed in detail and also with the help of multiple Python code examples.
What is Python Pandas Series Method
The series method is used to create a one-dimensional array that is labeled. The array can have various data like float, string, integer, etc.
Features of Pandas Series
- Labeled Indexing: In series, all the items have their specified index or label. By using them, we can easily access or modify the data.
- Homogeneous Data: A Series should consist of data of the same type which means it supports homogenous data.
- Flexibility: We can store various types of data in series. For instance, strings, numbers, or other complex objects that include functions or series.
- Size Immutable: It means the direct manipulation of the number of the series elements cannot be done. The size of the series is fixed when it’s created.
Examples Explaining the implementation and usage of Series
Ex.1 Creation of Pandas Series using List
In order to create a Series using a list, we can pass it a Python list as a parameter. See below:
import pandas as pd ls=[56,3,2,6,56.45,'Zee'] sr= pd.Series(ls) print(sr)
Output
0 56 1 3 2 2 3 6 4 56.45 5 Zee dtype: object
First, we import the panda’s library and gave it the short name ‘pd‘. Then we called the series method and passed the specified string to it. Printing the result shows that a series has been created. We got 2 columns in the output. The first column shows the index values while the second one represents the data. In the end, we can see the datatype ‘object’ as well. The reason is that we used different datatype items so they are stored in a object.
We can also create the series using a short method. See below:
srs= pd.Series([3,6,2,'st']) print(srs)
Output
0 3 1 6 2 2 3 st dtype: object
Ex.2 Series creating using Scalar(single) Value
sr1D=pd.Series(34) print(sr1D)
Output
0 34 dtype: int64
We just pass a single value to the pandas Series function and a series is created. If you want to create multiple items of the same data then use index. See below:
series1D=pd.Series(34,index=[1,2,3,4,5]) print(series1D)
Output
1 34 2 34 3 34 4 34 5 34 dtype: int64
See example 5 for a detailed guide on the role of the ‘index’ parameter in Series.
Ex.3 Create Series using Python Dictionary
dSeries=pd.Series({'name':'Zeeshan','age':26}) print(dSeries)
Output
name Zeeshan age 26 dtype: object
In this code, we passed a dictionary to the series and a series is created. The keys are set as index and the values are specified as data values.
Ex.4 Find the type of Series
sris=pd.Series(['Zeeshan','Furqan']) type(sris)
Output
pandas.core.series.Series
By using the type function, we can get/show the type of series. The output shows that it’s a 1D(one-dimensional) indexed array. It has data items with specified index values that start from 0(zero).
Ex.5 Creating an Empty Series
emS=pd.Series([]) print(emS)
Output
Series([], dtype: float64)
We passed an empty Python list to the series method and an empty series has been created. Also, we’ll get a warning that explicitly gives it a datatype which we can solve using the ‘dtype’ parameter. See below:
pd.Series([],dtype=int)
You can specify the datatype depending on your specific scenario. The warning will be gone by that.
Ex.6 Custom Index using the parameter ‘index’
sri=pd.Series([5,3,2],index=['x','y','z']) print(sri)
Output
x 5 y 3 z 2 dtype: int64
We can specify a custom index using the ‘index’ parameter. We can use integers, floats, or strings as the index. In the above example, we passed an index list. Just make sure that the length of both lists are same or else a value error will raise. It means that the number of data items should match the number of indexes.
Ex.7 Changing Datatype of Series Items
seriesVal=pd.Series([3,7,10],dtype=float) print(seriesVal)
Output
0 3.0 1 7.0 2 10.0 dtype: float64
In this code, we passed an integer items list but the output shows items in float. It’s because we used the ‘dtype’ parameter which can be used to change the data type of the Series. If we didn’t specify it in this example, then the datatype would have been of an integer as items are of integer datatype.
Ex.8 Custom name to Data Column using the ‘name’ Parameter
sVals=pd.Series(['grapes','pineapples','mangoes'],name='Fruits') print(sVals)
Output
0 grapes 1 pineapples 2 mangoes Name: Fruits, dtype: object
We can give a custom name to the data column using the ‘name parameter. In this code, we specified the name ‘Fruits’ to the data column and it’s shown in the output. In this way, you can give a more appropriate name to the data column in your own Python code.
Ex.9 Accessing data from Series using indexing
sris=pd.Series([6,3,4,2,3]) print(sris[2]) print(sris[0])
Output
4 6
We used the indexing method and passed a specific index. First examples search for index 2 as the index starts from 0 so it’ll return the 3rd item(0,1,2). In the second example, we specified the index 0 which will take the first item. By default, indexing starts from 0.
Ex.10 Slicing in Pandas Series
s=pd.Series([4,6,3,7,9,2]) print(s[2:5])
Output
Ex.11 Get maximum and minimum item from Series
ser=pd.Series([1,3,5,4,2,5]) print(min(ser)) print(max(ser))
Output
1 # using min() function 5 # using max function
In this code, we used the min() and max() functions to get the minimum and maximum item from the specified series.
Ex.12 Using conditional operations Pandas Series
sres=pd.Series([6,5,4,3]) sres[sres<5]
Output
2 4 3 3 dtype: int64
We can use conditional operators on series as well. In this code, the series will only have data items that are less than 5. See the below code as well:
sres[sres==4]
Output
2 4 dtype: int64
In this code, only those data items that are equal to 4 will be specified.
Images of Code Examples
Conclusion
In conclusion, we learned about the Python Pandas Series method in detail. We’ve discussed its parameters with code examples for a proper demonstration. Hope you like this article and have learned a lot from it.
Don’t forget to visit our other articles on other functions of Python Pandas and more. Thank you for reading it.