How to Create a Pandas Series Object in Python


In this article, we show how to create a pandas series object in Python.

A series object is an object that is a labeled list.

A series object is very similar to a list or an array, such as a numpy array, except each item has a label next to it. Another name for a label is an index.

The pandas module has this data called a series. All a series is is a labeled list, essentially.

Creating a series with the pandas module is very simple. All that is needed is the data. An optional second parameter is the labels that should appear next to each item in the list.

So let's go over a full example now of how to create a series with the pandas module in Python.

So let's now go over the code.

So we first have to import the pandas module. We do this with the line, import pandas as pd.

as pd means that we can reference the pandas module with pd instead of writing out the full pandas each time.

We create a variable, s1, which we set equal to, pd.Series([5,45,74])

When we only specify one parameter, this is the data. So we have data of 5,45,74. When we don't specify a second parameter (which would be the index or label), the index is automatically an integer index beginning with 0 and ascending up each item by 1.

We then create another variable, s2, for which we have 2 parameters. The first parameter is always the data of the series. The second parameter is the label, or index, of the series. The data of the series is, therefore, 1,68,45. The label, or index, is a,b,c.

In the next series, s3, we have float numbers, which you can see the type of below.

In the fourth and last series, s4, we have labels composed of city names and the data composed of float numbers.

Again, a series is a labeled list. It is used heavily in data science for various purposes.

You'll see later when we do dataframes that dataframes are composed of several series.

A series is can be created with lists, as seen above, but can also be created from numpy arrays.

This is shown in the code below.

So we originally created a numpy array and stored it in the variable, b1.

We then took this b1 variable and created a pandas series from it.

series1 has no index specified, so the default integer index, starting from 0, is created.

series2 has an index of x,y,z specified.

Another way we can create a panda series is through a dictionary, which is one of the easiest ways to create a pandas series.

This is because dictionaries are composed of key:value pairs.

Thus, when we put a dictionary in a pandas series, the key is the index. And the value is the data.

In the following code below, we have a dictionary which we convert into a pandas series.

So we have a dictionary stored in dict1.

With dictionaries, there is no need to specify an index (or label) because the keys of the dictionary automatically become the label. All we have to do is specify the dictionary within the pd.Series() function. This will turn a dictionary into a pandas series that contains an index and value for each item in the dictionary.

And this is how we can create a series object with the pandas module in Python.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...