Python

Pandas Create Series

A Python framework called Pandas makes upgrading and managing structured or labeled data easier. When dealing with lists, arrays, and dictionaries, Pandas offer a unique data structure called the series, similar to how the standard Python handles lists.

Pandas Series is a one-dimensional array that can store any Python-compatible data format and employs the labels to identify every input value for retrieving it. The labels that make up the index may either be strings or numbers. The primary data structure used by the Pandas framework to store a one-dimensional data is a series. Series is similar to a table’s column. It contains one or multiple rows of data items organized collectively by a common title.

Pandas Series Creation

The data we employ in Pandas tend to come from a certain source in general circumstances that are outside the Python environment. But in this article, we’ll concentrate on the information that comes from the file itself to keep things simple.

Lists, dicts, scalar data, and other data structures can be utilized to generate the Pandas Series. There are many ways to construct a series; few of them are given here.

The syntax for the creation of the Pandas Series is as follows:

Ensure that the Pandas library is imported before using any of the operations to generate the series.

Example 1: Creating a Basic Series

Because Pandas is a third-party library, we must initially import it into our Python file using the import Pandas declaration. The as pd language is then used in the script to refer to Pandas as “pd.” The Pandas’ library is now accessible to us, so we can start building our basic series.

The first script creates a new variable named “srs” and sets it to the output of invoking the pd.Series() function. The data that you wish to use to construct the series should be inserted inside the brackets, also known as the parameter. In this example, we create an empty series, so we leave the parenthesis of the pd.Series() function blank.

When you execute the script, your first empty Series appears. By using the print function to display the output to the terminal, you can verify that everything is accomplished.

This yields the following output displayed where the Series uses the float as its default data format.

Example 2: Generating a Series from a List

The first step in creating a Series from a list is to build the list and then the Series may be created from the list.

The Python list that served as the foundation for this series is declared inside parentheses ([ ]). We declared the list, assign it to the variable “ls,” and then sent the “ls” variable as a parameter to the pd.Series() method.

The following is the output of the print() statement. Here, the data type is declared as an object.

Example 3: Generating a Series from an Array

Another open-source Python package designed to facilitate analytical computations is NumPy. The NumPy array, which enhances the Python lists by optimizing space and efficiency, is one of its key features. There are no significant variations in the structure of Python lists and NumPy arrays because of these underlying discrepancies. This implies that the process for generating a series using an array is largely similar to one for generating a list, with the exception that you must import both the Pandas and the NumPy libraries.

We created a variable “info” just as in the prior illustrations. To create an array, you must execute the np. array() function in conjunction with incorporating the NumPy library along with the Pandas’ library, which is the only extra requirement from the list methodology. After that process is concluded, we call the pd.Series() method with the array (info) as an expression.

The results are consistent with what you may anticipate considering the list (ls) result.

The index of the Series commences at 0 and goes all the way up to -1, which is its length, by default.

Example 4: Generating a Series from an Array Having an Index

To establish a Series using a manually stated index rather than the default, the index argument requires a list of entries with an equal range of items as the array’s size.

Here, we assign the “day” dataset a customized list ([“s”, “u”, “n”, “d”, “a,” “y”) by supplying it as the input for the index argument.

The output is shown in the following:

The list you specified while generating the series has now taken the place of the default 0 indexed tags (0, 1, 2, 3). The most prevalent label, however, strings because it provides access to a completely different search technique.

Example 5: Generating a Series from a Dictionary

Dictionaries are groups of data that are structured as key combinations in Python. Only one data value is associated with each key, which is unique. A sample can be seen in the following series approach:

We declare a dictionary using the curly braces {} much like we do while we build a list. In this case, the variable “dt” is allocated to refer to the object. The key-value pairs are contained inside the “dt”. For instance, the “5” value key is “pandas”. The pd.Series() function is now invoked with the dictionary as the parameter.

When you call the print statement, you will get the output shown in the following image:

You could observe a difference between this example and the others. The default index tags are swapped with the keys in our dictionary. Now, we can retrieve the values from our series based on either its numeric or string label. Dictionary transformation to series is a quick and easy technique to specify a unique index and keep the key values.

Example 6: Generating a Series from Scalar Values

An index should be specified to generate a series if the input is a scalar value. To correspond with the index’s length, this value would be replicated.

The terminal shows the following output when you run the print statement:

Example 7: Generating a Series from NumPy Function

NumPy has a variety of functions that can be employed to build Series, including the numpy.linspace() and numpy.random.radn() functions. In this illustration, we will use the numpy.linspace() method to create the Series.

In the previous code snippet, we initialized two variables: “sr1” and “sr2”. We invoked the np.linspace() function inside the braces of the pd.series function and passed the 3 parameters – lowest value, highest value, and the length of the array. Then, we employed the print() statement to display both the Series, one after another.

The resultant screen is given in the following:

Conclusion

In today’s discussion, we have chosen the topic “pandas.series()” function. We divided the article into two sections: introduction and examples. In the first part of the article, we introduced you to the Pandas Series: what they are and why we use them in Python. In the next section, we defined and explained the different methods to create a Series. We elaborated on the seven different techniques to generate a Series using the Pandas Series function and tried to explain all the steps carried out in the example codes that we used for practical implementation on Spyder. We hope that this article helps you in learning the Pandas Series module.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.