Series is a one-dimensional labeled array in pandas capable of holding data of any type (integer, string, float, python objects, etc.). In this tutorial we will learn the different ways to create a series in python pandas (create empty series, series from array without index, series from array with index, series from list, series from dictionary and scalar value ). The axis labels are called as indexes. The different ways of creating series in pandas are
- Create an Empty Series in pandas
- Create a series from array without indexing
- Create a series from array with indexing
- Create a series from dictionary
- Create a series from scalar value
- Create a series from list in pandas
- Create Series from multi list
Multiple series can be combined together to create a dataframe
Create an Empty Series:
A basic series, which can be created is an Empty Series. Below example is for creating an empty series.
# Example Create an Empty Series import pandas as pd s = pd.Series() print s
output:
Create a series from array without index:
Lets see an example on how to create series from an array.
# Example Create a series from array import pandas as pd import numpy as np data = np.array(['a','b','c','d','e','f']) s = pd.Series(data) print s
output:
Create a series from array with index:
This example depicts how to create a series in python with index, Index starting from 1000 has been added in the below example.
# Example Create a series from array with specified index import pandas as pd import numpy as np data = np.array(['a','b','c','d','e','f']) s = pd.Series(data,index=[1000,1001,1002,1003,1004,1005]) print s
output:
Create a series from Dictionary:
This example depicts how to create a series in python with dictionary. Dictionary keys are used to construct index.
# Example Create a series from dictionary import pandas as pd import numpy as np data = {'a' : 0., 'b' : 1., 'c' : 2.} s = pd.Series(data,index=['b','c','d','a']) print s
Index order is maintained and the missing element is filled with NaN (Not a Number). So the output will be
output:
Create a series from Scalar value:
This example depicts how to create a series in python from scalar value. If data is a scalar value, an index must be provided. The value will be repeated to match the length of index
# create a series from scalar import pandas as pd import numpy as np s = pd.Series(7, index=[0, 1, 2, 3]) print s
output:
Create a series from List:
This example depicts how to create a series in pandas from the list. pd.series() takes list as input and creates series from it as shown below
# create a series from list import pandas as pd # a simple list list = ['c', 'v', 'e', 'v', 's'] # create series form a list ser = pd.Series(list) ser
output:
Create a series from Multi list:
This example depicts how to create a series in pandas from multi list. pd.series() takes multi list as input and creates series from it as shown below
# create a series from multi list import pandas as pd # multi-list list = [ ['datascience'], ['made'], ['simple'], ['is'], ['a'], ['blog'], ['for'], ['datascience'],['professional'] ] # create Pandas Series ser = pd.Series((i[0] for i in list)) ser
output: