Pandas Series

In this article, we will explore what Pandas Series is, how it works, and how to use it with examples.

Pandas is a popular data analysis library in Python that provides several data structures to work with tabular and time-series data. One of the most commonly used data structures in Pandas is the Series.



Pandas Series – What is it?

A Pandas Series is a one-dimensional array-like object that can hold any data type. It is similar to a Python list or an array, but with some additional functionality and features.

A Series consists of two main parts – an index and a data array.

The index is a set of labels that identify each element of the data array. By default, the index is a sequence of integers starting from 0, but it can also be specified to be any other type of values, such as dates, strings, or even other arrays.


How to create a Pandas Series?

A Pandas Series can be created from a list, a dictionary, an array, or any other sequence-like object.

There is an analogy between a Pandas Series and a column in a table.

It is a one-dimensional array that can hold any kind of data, regardless of its type.

To create a Series, we can use the Series() constructor function.

Utilize the capitals list to make a basic Pandas series with the Series() method:

Example: 

import pandas as pds capitals = ["Washington","London","Berlin","Paris","Islamabad","Rome"] mrx = pds.Series(capitals) print(mrx)

Here is an example of how you can build a basic Pandas Series from a list:

Example: 

import pandas as pds leagues = ["Major League Soccer","Premier League","La Liga","Serie A","Süper Lig"] mrx = pds.Series(leagues) print(mrx)

Labels

Values are labeled with their index numbers if no other information is provided. The first element contains index 0, the second element contains index 1, etc.

A particular item can be obtained through this label.

Retrieve the first item from the capitals series:

Example: 

import pandas as pds capitals = ["Washington","London","Berlin","Paris","Islamabad","Rome"] mrx = pds.Series(capitals) print(mrx[0])

From the leagues series display the initial three items:

Example: 

import pandas as pds leagues = ["Major League Soccer","Premier League","La Liga","Serie A","Süper Lig"] mrx = pds.Series(leagues) print(mrx[0]) print(mrx[1]) print(mrx[2])

Create Labels

You can give names to your custom labels through the index argument.

You can make your personal labels as follows:

Example: 

import pandas as pds evenNum = [0, 2, 4, 6, 8, 10] mrx = pds.Series(evenNum, index = ["a", "b", "c", "d", "e", "f"]) print(mrx)

With the optional argument index customize your own label:

Example: 

import pandas as pds palindrome = [101, 252, 14541, 333, 87979, 21112] mrx = pds.Series(palindrome, index = ["p1", "p2", "p3", "p4", "p5", "p6"]) print(mrx)

Through your custom-made labels, you can locate items by identifying them on their labels.

Display the value of index “a”:

Example: 

import pandas as pds evenNum = [0, 2, 4, 6, 8, 10] mrx = pds.Series(evenNum, index = ["a", "b", "c", "d", "e", "f"]) print(mrx["a"])

Show the palindromes at “p2”, “p3” and “p5” locations:

Example: 

import pandas as pds palindrome = [101, 252, 14541, 333, 87979, 21112] mrx = pds.Series(palindrome, index = ["p1", "p2", "p3", "p4", "p5", "p6"]) print(mrx["p2"]) print(mrx["p3"]) print(mrx["p5"])

Key/Value Objects as Series

When making a series, you can additionally utilize a key-value object, such as a dictionary.

Apply the billionaires dictionary to make a basic Pandas series with the Series() method:

Example: 

import pandas as pds billionaires = {"1": "Bernard Arnault", "2": "Elon Musk", "3": "Bill Gates", "4":"Jeff Bezos"} mrx = pds.Series(billionaires) print(mrx)
Reminder: In the dictionary, the keys represent the labels, and the labels represent the words.

The following is an example of how you can generate a basic Pandas Series from a dictionary:

Example: 

import pandas as pds leagues = {"United States of America":"Major League Soccer", "England":"Premier League", "Spain":"La Liga","Italy":"Serie A"} mrx = pds.Series(leagues) print(mrx)

It is possible to select only a few of the elements in the dictionary through the index argument, and then indicate only the elements that you want to appear in the series.

Make a series utilizing only values from “2” and “3” in the following dictionary:

Example: 

import pandas as pds billionaires = {"1": "Bernard Arnault", "2": "Elon Musk", "3": "Bill Gates", "4":"Jeff Bezos"} mrx = pds.Series(billionaires, index = ["2", "3"]) print(mrx)

In the following leagues dictionary generate a series whose data is only from the below indexes.

Example: 

import pandas as pds leagues = {"United States of America":"Major League Soccer", "England":"Premier League", "Spain":"La Liga","Italy":"Serie A"} mrx = pds.Series(leagues, index = ["United States of America", "England", "Spain"]) print(mrx)

DataFrames

A Pandas data sets is commonly a multi-dimensional table referred to as a DataFrame.

It is important to note that a Series is similar to a column, while a DataFrame is like a table in its entirety.

Build a DataFrame billionaires_data from series “rank” and “name”:

Example: 

import pandas as pds billionaires_data = { "rank": [1, 2, 3, 4], "name": ["Bernard Arnault", "Elon Musk", "Bill Gates", "Jeff Bezos"] } mrx = pds.DataFrame(billionaires_data) print(mrx)

Make a data set of leagues_data by implementing the DataFrame() function:

Example: 

import pandas as pds leagues_data = { "Country": ["United States of America", "England", "Spain", "Italy"], "League name": ["Major League Soccer", "Premier League", "La Liga", "Serie A"] } mrx = pds.DataFrame(leagues_data) print(mrx)

Example Explanation

The above code imports the pandas library and creates a dictionary leagues_data that contains information about four different soccer leagues in different countries.

The keys in the dictionary represent the different attributes of the leagues such as “Country” and “League name”, and the values associated with these keys are lists of strings that contain the actual data.

The code then uses the pds.DataFrame() function to convert the dictionary leagues_data into a Pandas DataFrame mrx. This DataFrame has two columns “Country” and “League name”, with the values from the original dictionary as rows.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *