How to Create Series in Python Pandas

In this article, we will learn different ways of creating Pandas Series.

How to Create Series in Python Pandas

Hello,

And welcome back to Data Analysis in Python with Pandas Series. In the last article, we learned about Pandas and how it is a great library for data analysis in Python with some introduction to the two primary data structures of Pandas.

In this article, we will learn one of the Pandas data structures: SERIES.

So, let's get started

Series

Let's start with, recalling what series is? it is a one-dimensional ndarray with homogenous data with axis labels which are referred to as Index, capable of holding any data type (integers, strings, floating-point numbers, etc.).

we can create a Pandas series using this syntax:

pandas.Series(data, index, dtype=none, name=none)

Here

  • data can be a list, dictionary, ndarray, or a scalar value.

  • The index can be a list of values but if not specified, the default index (0, 1, ... len(data)-1) will be assigned.

  • dtype is a datatype of output Series. If not specified, this will be inferred from data.

  • And name is a name to give to a series.

Series with Different Data Inputs

Before we start with creating Series, we need to load two important libraries in our namespace, so let's start with importing NumPy and Pandas libraries.

libraries.png

Using List

We can create a Pandas Series using a list without specifying an index.

For example,

UsingList.png

Did you notice?, The default index from 0 to len(data) - 1 is assigned, since the length of data is 3 the index assigned was 0, 1, 2. Now let's create a series using a list with a specified index.

UsingListWithIndex.png

Using Dictionary

Series can be generated from the dictionary. if data is dictionary type and index is not passed, the Series index will be the dictionary key.

For example:

UsingDictionary.png

But if you specified the index then values in the dictionary for corresponding to the labels in the index will be pulled out.

Let's see how

UsingDictionaryWithIndex.png

So we can see that, if the labels in the index are in the dictionary, then the corresponding value to that label is assigned but if the label is not in the dictionary as a key then 'NaN' will be assigned as a value. NaN is short for Not a Number. It is a standard marker for the missing values in Pandas.

Using Ndarray

We can create pandas series from NumPy's ndarray. so first let's start with creating a NumPy array and assigned it as data in Series.

UsingNdarray.png

Using Scalar Value

If data is a scalar value, an index must be provided. The value will be repeated to match the length of the index.

UsingScalar.png

The 'name' attribute

Pandas Series Name parameter allows you to give a name to series.

nameatt.png

The 'dtype' attribute

We can specify the datatype of data in the series with the help of the dtype parameter of Pandas Series.

dtype.png

This is all about today's blog, we learned different ways of creating Series. I hope this helps you.

Thank you for reading !! See you in my next article. Take care. :)

Did you find this article valuable?

Support Madhuri's blog by becoming a sponsor. Any amount is appreciated!