Pandas Series
pandas Series is a one-dimensional labelled data structure which can hold data such as strings, integers and even other Python objects.
#pandas series # pandas create series
A pandas Series is a one-dimensional labelled data structure which can hold data such as strings, integers and even other Python objects. It is built on top of numpy array and is the primary data structure to hold one-dimensional data in pandas.
In Python, a pandas Series can be created using the constructor pandas.Series().
pandas.Series
Syntax
-
- pandas.Series(data=None, index=None, dtype=None, name=None, copy=False)
Purpose
-
- To create a one-dimensional data structure which can store different types of data
Parameters
-
- data (array or dict or scalar value or iterables (default:None). It is used to populate the rows of the Series object.)
- index (array or index (default: None). It is used to label the rows of the Series. It’s length must be the same as the object passed in the data parameter and all the values must be unique. It’s default value is ‘None’ but if the index parameter is not passed then a range of integer values from zero to one less than the total number of rows in the data object will be considered as the index.)
- dtype (String or numpy.dtype or ExtensionDtype (default: None). It is used to specify the data type of the Series which will be formed. If this parameter is not specified then the data type will be inferred from the values present in the series)
- name (String (default: None). It is used to give a name to the Series)
- copy (Boolean (default: False). It is used to copy the input data)
Note: If you check the offical documentation for pandas Series then another parameter called fastpath will be mentioned there. However, that parameter is an internal parameter and it cannot be actually modified by the user
How to create a pandas series from a list
A pandas Series can be created from a list by passing the list to the data parameter.

As you can see, a pandas Series has been created from the list. Since no indices were passed, integer values from zero to one less than the total number of rows (five in this case) have been assumed as indices.
Making a pandas series from a tuple
Creating a pandas Series from a tuple is similar to creating a Series from a list. Make the tuple which contains the required data, and then pass it to the data parameter of the series constructor.

Making a pandas series from dictionaries
For creating a Series from a dictionary, the indices of the Series are formed from the keys of the dictionary, while the values of the Series are formed from the values passed to the corresponding keys of the dictionary.
After creating the dictionary, pass it to the data parameter of the series constructor to make the pandas Series.

Making a pandas series using numpy array
Pandas Series can also be made using Numpy arrays.
Numpy offers certain methods for creating arrays having random values or values within a specified range.
Then, these arrays can be converted to Series as well.
Creating a series from a Numpy array
For creating a Series from a Numpy array, simply pass the array to the data parameter of the series constructor.

Using the Numpy random.randn function
This function is used to create a Numpy array of specified dimensions and the array will contain random values sampled from a univariate normal distribution.
After making the array, pass it to the data parameter of the series constructor to make the Series.

Using the Numpy linspace function
This function is used to create an array having values within a certain range.
After creating the array, pass the array to the data parameter of the series constructor to create the pandas Series.

Using the arange function
This function is used to create Numpy arrays, which in turn will be used to create a Series having values within a certain range. The values are also spaced equally within the given range.
After creating the array using this function, pass it to the data parameter of the series constructor to create the Series.

Using multi-dimensional Numpy arrays
Series can also be created using multi-dimensional Numpy arrays.
However, after making the multi-dimensional array, first convert the array into a nested list, and then pass the list to the data parameter of the series constructor.
![]()
Creating a series using scalar value
Pandas Series can also be created by using a single scalar value.
In this case, you need to explicitly pass the indices as well in order to determine the length of the Series.

Making a series with a defined index
To create a Series with a pre-defined index, pass the list of indices to the index parameter.

Conclusion
In this article, you saw the different ways to create a pandas Series using:
- Lists
- Tuples
- Dictionaries
- Numpy arrays
- Scalar value
- Defined index
Practical Tips
- Multi-dimensional Numpy arrays will first have to be converted to a list and then into a Series. The multi-dimensional array cannot be directly converted into a Series.
- The index values must be hashable and the same length as the object passed to the data parameter.
Test Your Knowledge
Q1: Pandas Series cannot be formed using tuples. True or False?
Answer
Answer: False, they can be made using tuples
Q2: How is the data type of a pandas Series determined if the dtype parameter is not explicitly passed?
Answer
Answer: The data types are inferred from the values present in the Series.
Q3: Identify the error in the given code and write the correct code:
new_series = pd.Series(data=[1,2,3,4,5,6,7,8,9],index=[1,2,3,4,5,5,6,7,8,9]
Answer
Answer:
new_series = pd.Series(data=[1,2,3,4,5,6,7,8,9],index=[1,2,3,4,5,6,7,8,9]
Show
Q4: Write the code to make a Series from a Numpy array whose values are integers ranging from 1 to 10 using the arange function.
Answer
Answer: pd.Series(np.arange(1,11,1))
Show
Q5: Convert a 3×3 Numpy array whose values are the first 9 multiples of the number 9 into a pandas Series.
Answer
Answer:`ndarray = ([[9,18,27], [36,45,54], [63,72,81]])
nd_list = ndarray.tolist()
nd_series = pd.series(nd_list)
This article was contributed by Shreyansh.