AppDividend
Latest Code Tutorials

Python Pandas Series Tutorial | Data Structure Example In Pandas

0

Python Pandas Series Tutorial | Data Structure Example In Pandas is today’s topic. Pandas Series is the one-dimensional labeled array capable of holding any data type. Series is the one-dimensional labeled array capable of carrying data of any data type like integer, string, float, python objects, etc. The axis labels are collectively called index. In layman’s terms, Pandas Series is nothing but the column in an excel sheet.

Python Pandas Series Tutorial

Series is a one-dimensional data structure designed for the particular use case. You can control the index(label) of elements. Series label can be thought of similar to the python dictionary.  Series also support the vector operations. Any operation to perform on the series, get’s performed on every single element.

How to Create a Series in Pandas?

The Pandas Series can be created out of the Python list or NumPy array. 

Unlike Python lists, the Series will always contain data of the same type. This makes NumPy array the better candidate for creating a pandas series.

Let’s create a series using NumPy library.

# app.py

import pandas as pd
import numpy as np

data = np.array(['a','b','c','d'])
seri = pd.Series(data)
print(seri)

In the above example, we have imported two libraries which are Pandas and Numpy.

Then we have used the NumPy to construct the data and passed that to the series function of pandas and created a series.

Run the above file and see the output.

 

Python Pandas Series Tutorial

Syntax

A pandas Series can be created using the following constructor.

pandas.Series( data, index, dtype, copy)

The data parameter takes various forms like ndarray, list, constants.

The index parameter values must be unique and hashable, the same length as data.

The dtype parameter is for the data type. If None, data type will be inferred.

The copy parameter is to copy the data. The default parameter is False.

Create an Empty Series

A primary series, which can be created is an Empty Series. See the following code.

# app.py

import pandas as pd

seri = pd.Series()
print(seri)

See the below output.

 

Create an Empty Series

Create a Series from ndarray

If data is a ndarray, then index passed must be of the same length. If no index is passed, then by default index will be range(n) where n is array length, i.e., [0,1,2,3…. range(len(array))-1].

# app.py

import pandas as pd
import numpy as np

data = np.array(['A','B','C','D'])
seri = pd.Series(data)
print(seri)

In this example, we have imported the NumPy library and created a data array and pass that data to the series function to create a Pandas Series.

 

Create a Series from ndarray

If we did not pass any index, by default, it would be assigned the indexes ranging from 0 to len(data)-1, i.e., 0 to 3.

Provide the Indexes With Data in Series

Let’s take an example where we pass the data as well as indexes and see the output.

# app.py

import pandas as pd
import numpy as np

data = np.array(['A','B','C','D','E'])
seri = pd.Series(data, index=[18, 19, 20, 21, 22])
print(seri)

See the output below.

 

Provide the Indexes With Data in Series

Create a Series from the dictionary

dictionary can be passed as input, and if there is no index is specified, then the dictionary keys are taken in the sorted order to construct an index.

If an index is passed, the values in data corresponding to the labels in the index will be pulled out.

# app.py

import pandas as pd
import numpy as np

data = {'name' : 'krunal', 'website' : 'appdividend.com', 'role' : 'author'}
siri = pd.Series(data)
print(siri)

We have taken the Python Dictionary as a data. See the below output.

 

Create a Series from the dictionary

Create a Series from Scalar

If data is the scalar value, then an index must be provided. 

The value will be repeated to until the length of the index. See the following example.

# app.py

import pandas as pd
import numpy as np

siri = pd.Series(21, index=[0, 1, 2, 3, 4])
print(siri)

See the below output.

 

Create a Series from Scalar

Accessing Data from Series

We can access the items through its index. 

Data in the series can be accessed similarly to that in a ndarray.

See the below example.

# app.py

import pandas as pd
import numpy as np

data = np.array(['A','B','C','D','E'])
seri = pd.Series(data, index=[18, 19, 20, 21, 22])
print(seri[20])

In the above example, we have already provided the indexes which starts from 18 to 22. We get the output because the index maps to that element. Now, see the below output.

 

Accessing Data from Series in Pandas

Finally, Python Pandas Series Tutorial | Data Structure Example In Pandas is over.

 

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.