Building Pandas Series with Several Datatypes

A Pandas Series is a one-dimensional array-like data structure that is part of the Python Pandas library and it is capable of holding any data type, such as integers, floats, strings, and even other Python objects. The best resemblance of it is to a column in a spreadsheet or a database table, with a labeled index that can be used to access individual elements. In this thread, you’ll learn different ways of creating this Series using the following data types:

  1. Lists
  2. NumPy arrays
  3. Dictionaries

Using a list to create a series

In Python, a list is an ordered collection of items or elements that can be of different types. Lists are mutable, meaning that the elements can be modified after the list is created and a list is created using square brackets [ ], with the elements separated by commas. Here is how you can create a Pandas series using a list:

1. By using "pd.Series()" constructor with default index:

Note: The default index of Python starts from 0 and so on, that’s why the index is starting from 0.

2. By using "pd.Series()" constructor with custom index:

Note: The index list size should be the same as the size of the list of elements.

Using an array to create a series

In Python, a NumPy array is a grid of values, all of the same type, and is indexed by a tuple of positive integers. NumPy arrays are similar to Python lists but offer much more functionality, including the ability to perform mathematical operations on multiple elements simultaneously. NumPy arrays can be one-dimensional or multi-dimensional. Here is how you can create a series using arrays:

1. By using "pd.Series()" constructor with default index and creating an array using "arange()" function:

  • “arange()” is a function in the NumPy library that returns an array with evenly spaced values within a specified interval.

Note: The “step” argument (3rd argument) in “arange()” function defines the spacing between two consecutive elements in the array. The default value is 1.

2. By using "pd.Series()" constructor with custom index and creating a custom array:

Note: The index list size should be the same as the size of the array of elements.

Using a dictionary to create a series

In Python, a dictionary is an unordered collection of key-value pairs. It is sometimes referred to as an associative array, hash table, or map. The keys in a dictionary must be unique and immutable (i.e. strings, numbers, or tuples that contain only immutable elements), and the values can be of any type. They are created using curly braces { }, with key-value pairs separated by a colon : and individual pairs separated by a comma.

1. By using "pd.Series()" constructor with default ordered index:

Note: The order of the index is the same as the order of the keys in the dictionary.

2. By using "pd.Series()" constructor with custom ordered index:

Note: The order of the index is different from the order in the dictionary. Also, remember to add only those elements in your index list which are already present as keys in the dictionary, if values that are not keys are added, then it’d return “NaN” in the series corresponding to those index values.

3. By using "pd.Series()" with a filtered dictionary:

1 Like