In the Python Pandas library, a Series is one of the primary data structures, that offers a convenient way to handle and manipulate one-dimensional data. It is similar to a column in a spreadsheet or a single column in a database table. In this tutorial you will learn more about Pandas Series and use Series effectively for data manipulation and analysis.
What is a Series?A Series in Pandas is a one-dimensional labeled array capable of holding data of any type, including integers, floats, strings, and Python objects. It consists of two main components −
A Series is similar to a one-dimensional ndarray (NumPy array) but with labels, which are also known as indices. These labels can be used to access the data within the Series. By default, the index values are integers starting from 0 to the length of the Series minus one, but you can also manually set the index labels.
Creating a Pandas SeriesA pandas Series can be created using the following constructor −
class pandas.Series(data, index, dtype, name, copy)
The parameters of the constructor are as follows −
Sr.No Parameter & Description 1data
Data takes various forms like ndarray, list, or constants.
2index
Index values must be unique and hashable, with the same length as data. Default is np.arange(n) if no index is passed.
3dtype
Data type. If None, data type will be inferred.
4copy
Copy data. Default is False.
A series object can be created using various inputs like −
If no data is provided to the Series constructor pandas.Series() it will create a basic empty series object.
ExampleFollowing is the example demonstrating creating the empty Series.
#import the pandas library and aliasing as pd import pandas as pd s = pd.Series() # Display the result print('Resultant Empty Series:\n',s)
Its output is as follows −
Resultant Empty Series: Series([], dtype: object)Create a Series from ndarray
An ndarray is provided as an input data to the Series constructor, then it will create series with that data. If you want specify the custom index then index passed must be of the same length of input data. If no index is specified, then Pandas will automatically generate a default index from staring 0 to length of the input data, i.e., [0,1,2,3. range(len(array))-1].
ExampleHere's the example creating a Pandas Series using an ndarray.
#import the pandas library and aliasing as pd import pandas as pd import numpy as np data = np.array(['a','b','c','d']) s = pd.Series(data) print(s)
Its output is as follows −
0 a 1 b 2 c 3 d dtype: object
We did not pass any index, so by default, it assigned the indexes ranging from 0 to len(data)-1, i.e., 0 to 3.
ExampleThis example demonstrates applying the custom index to the series object while creating.
#import the pandas library and aliasing as pd import pandas as pd import numpy as np data = np.array(['a','b','c','d']) s = pd.Series(data,index=[100,101,102,103]) print("Output:\n",s)
Its output is as follows −
Output: 100 a 101 b 102 c 103 d dtype: object
In this example we have provided the index values. Now we can observe the customized indexed values in the output.
Create a Series from Python DictionaryA dictionary can be passed as input to the pd.Series() constructor to create a series with the dictionary values. If no index is specified, then the dictionary keys are taken in a sorted order to construct the series index. If index is passed, the values in data corresponding to the labels in the index will be pulled out.
Example 1Here is the basic example of creating the Series object using a Python dictionary.
#import the pandas library and aliasing as pd import pandas as pd import numpy as np data = {'a' : 0., 'b' : 1., 'c' : 2.} s = pd.Series(data) print(s)
Its output is as follows −
a 0.0 b 1.0 c 2.0 dtype: float64
Observe − Dictionary keys are used to construct index.
Example 2In this example a Series object is created with Python dictionary by explicitly specifying the index labels.
#import the pandas library and aliasing as pd import pandas as pd import numpy as np data = {'a' : 0., 'b' : 1., 'c' : 2.} s = pd.Series(data,index=['b','c','x','a']) print(s)
Its output is as follows −
b 1.0 c 2.0 d NaN a 0.0 dtype: float64
Observe − Index order is persisted and the missing element is filled with NaN (Not a Number).
Create a Series from ScalarIf you provide a single scalar value as data to the Pd.Series() constructor with specified index labels. Then that single value will be repeated to match the length of provided index object.
ExampleFollowing is the example that demonstrates creating a Series object using a single scalar value.
#import the pandas library and aliasing as pd import pandas as pd import numpy as np s = pd.Series(5, index=[0, 1, 2, 3]) print(s)
Its output is as follows −
0 5 1 5 2 5 3 5 dtype: int64
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4