Pandas – Create Pandas Series
Create Pandas series– In this tutorial, we are going to create pandas series. A pandas series is like a NumPy array with labels that can hold an integer, float, string, and constant data.
Syntax
pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False) where data : array-like, Iterable, dict, or scalar value index : array-like or Index (1d) dtype : str, numpy.dtype, or ExtensionDtype, optional name : str, optional copy : bool, default False
Creating a Blank Pandas Series
#blank series import pandas as pd s = pd.Series() print(s)
Output of the code
Series([], dtype: float64)
Note: float64 is the default datatype of the Pandas series.
Series with numbers
#series with numbers import pandas as pd s = pd.Series([10, 20, 30, 40, 50]) print(s)
Output of the above code
0 10 1 20 2 30 3 40 4 50 dtype: int64
series with numbers and index
#series with numbers and index import pandas as pd s = pd.Series([10, 20, 30, 40, 50], index=[1, 2, 3, 4, 5]) print(s)
Output
1 10 2 20 3 30 4 40 5 50 dtype: int64
series with numbers and char index
#series with numbers and char index import pandas as pd s = pd.Series([10, 20, 30, 40, 50], index=['a', 'b', 'c', 'd', 'e']) print(s)
output
a 10 b 20 c 30 d 40 e 50 dtype: int64
series with constant values
#series with constant values import pandas as pd s = pd.Series(55, index=[1, 2, 3, 4, 5, 6]) print(s)
output
1 55 2 55 3 55 4 55 5 55 6 55
series with constant and python function
#series with constant and python function import pandas as pd s = pd.Series(34, index=range(100)) print(s)
output
0 34 1 34 2 34 3 34 4 34 .. 98 34 99 34 Length: 100, dtype: int64
series with python function
# series with python function import pandas as pd s = pd.Series(range(2, 89)) print(s)
output
0 2 1 3 2 4 3 5 4 6 .. 82 84 83 85 84 86 85 87 86 88 Length: 87, dtype: int64
series with float values
If any value of the data is float then the system automatically convert the datatype of whole series into float.
# series with float values import pandas as pd s = pd.Series([10, 20, 30, 40.5, 50]) print(s)
Output
0 10.0 1 20.0 2 30.0 3 40.5 4 50.0 dtype: float64
series with string type values
# series with string type values import pandas as pd s = pd.Series('Welcome to DAV Chander Nagar‘ , index=[1, 2, 3, 4, 5, 6]) print(s)
output
1 Welcome to DAV Chander Nagar 2 Welcome to DAV Chander Nagar 3 Welcome to DAV Chander Nagar 4 Welcome to DAV Chander Nagar 5 Welcome to DAV Chander Nagar 6 Welcome to DAV Chander Nagar dtype: object
series with string and index also in string
# series with string and index also in string import pandas as pd s = pd.Series('Welcome to DAV Chander Nagar', index=['rakesh', 'arushi', 'mannat', 'vinay', 'pratham']) print(s)
output
rakesh Welcome to DAV Chander Nagar arushi Welcome to DAV Chander Nagar mannat Welcome to DAV Chander Nagar vinay Welcome to DAV Chander Nagar pratham Welcome to DAV Chander Nagar dtype: object
NOTE : The data type in this case is object.
series with range and for loop
# series with range and for loop s = pd.Series(range(5), index=[x for x in 'abcde']) print(s)
output
0 34 1 34 2 34 3 34 4 34 .. 98 34 99 34 Length: 100, dtype: int64
series with range and for loop
# series with range and for loop s = pd.Series(range(5), index=[x for x in 'abcde']) print(s)
output
a 0 b 1 c 2 d 3 e 4 dtype: int64
NOTE: List comprehension has been used to create a list.
Pandas series with two different lists
# series with two different lists import pandas as pd names = ['rakesh', 'vishank', 'nikunj', 'unnati', 'vipul'] city = ['GZB', 'Delhi', 'Meerut', 'Pune', 'Panji'] s = pd.Series(names, index=city) print(s)
output
GZB rakesh Delhi vishank Meerut nikunj Pune unnati Panji vipul dtype: object
Create Pandas series with Nan values of numpy
#series with Nan values of numpy import pandas as pd import numpy as np s = pd.Series([10, 20, 30, np.NaN, -34.5, 6]) print(s)
output
0 10.0 1 20.0 2 30.0 3 NaN 4 -34.5 5 6.0 dtype: float64
series from a python Dictionary
#series from a python Dictionary import pandas as pd dict1 = {'name': 'rakesh', 'roll': 20, 'city': 'Gzb', 'age': 40, 'profession': 'Teaching'} s = pd.Series(dict1) print(s)
output
name rakesh roll 20 city Gzb age 40 profession Teaching dtype: object
Pandas Series using NumPy arange( ) function
import pandas as pd import numpy as np data = np.arange(10, 15) s = pd.Series(data**2, index=data) print(s)
output
10 100 11 121 12 144 13 169 14 196 dtype: int32
Hope these examples will help to create Pandas series. In the above examples, the pandas module is imported using as. You can learn more about importing a Python module in the Python Module tutorial.