#pandas series # pandas create series
A pandas Series is a onedimensional 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 onedimensional 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 onedimensional 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
 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
Free Time Series Project Template
Do you want learn how to approach projects across different domains with Time Series?
Get started with your first Time Series Industry Project and Learn how to use and implement algorithms like ARIMA, SARIMA, SARIMAX, Simple Exponential Smoothing and HoltWinters.
Do you want learn how to approach projects across different domains with Time Series?
Get started with your first Time Series Industry Project and Learn how to use and implement algorithms like ARIMA, SARIMA, SARIMAX, Simple Exponential Smoothing and HoltWinters.
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.
# Make a series from a list
import pandas as pd
# Create the list
data_list = ['Jeff Bezos', 'Elon Musk',
'Bernard Arnault', 'Bill Gates', 'Warren Buffett']
# pass the list to the data parameter of the series constructor
series = pd.Series(data=data_list)
series
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.
# Create the tuple
data_tuple = tuple(
['Jeff Bezos', 'Elon Musk', 'Bernard Arnault', 'Bill Gates', 'Warren Buffett'])
# Pass the tuple to the data parameter of the series constructor
series = pd.Series(data=data_tuple)
series
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.
# Make the dictionary
data_dict = {0: 'Jeff Bezos', 1: 'Elon Musk',
2: 'Bernard Arnault', 3: 'Bill Gates', 4: 'Warren Buffett'}
# Pass the dictionary to the data parameter of the series constructor
series = pd.Series(data=data_dict)
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.
# Create the series from array
import numpy as np
data_nparray = np.array(
['Jeff Bezos', 'Elon Musk', 'Bernard Arnault', 'Bill Gates', 'Warren Buffett'])
series = pd.Series(data=data_nparray)
series
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.
# Create the numpy array
# The parameter '5' indicates that the dimensions of the arrays is (5,1)
data_nparray_random = np.random.randn(5)
# pass the array to the data parameter of the series constructor
series = pd.Series(data_nparray_random)
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.
# Create the array using linspace
# the first two parameters define the range and the third parameter defines the length of the array
data_nparray_linspace = np.linspace(1, 10, 5)
# pass the array to the data parameter of the series constructor
series = pd.Series(data_nparray_linspace)
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.
# The first two parameters define the range while the third parameter defines
# the interval at which the values are inserted
data_arange = np.arange(1, 5, 1)
# Pass the array to the data parameter of the series constructor
series = pd.Series(data_arange)
series
Using multidimensional Numpy arrays
Series can also be created using multidimensional Numpy arrays.
However, after making the multidimensional array, first convert the array into a nested list, and then pass the list to the data parameter of the series constructor.
# Create the multidimensional array
ndarray = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Convert the array to a list using the tolist method
nd_list = ndarray.tolist()
# Convert the list
nd_series = pd.Series(nd_list)
nd_list
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.
# Create series from scalar value
data = 5
# Passing the value to the data parameter of the series constructor
series = pd.Series(data, index=[0, 1, 2, 3, 4])
series
Making a series with a defined index
To create a Series with a predefined index, pass the list of indices to the index parameter.
# Create series from a list and defined index.
data_list = ['Jeff Bezos', 'Elon Musk',
'Bernard Arnault', 'Bill Gates', 'Warren Bufett']
# Create the indices
indices = ['Amazon', 'Tesla', 'Louis Vuitton',
'Microsoft', 'Berkshire Hathaway']
# Pass them to the series constructor
series = pd.Series(data=data_list, index=indices)
series
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
 Multidimensional Numpy arrays will first have to be converted to a list and then into a Series. The multidimensional 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]
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))
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.