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:
Here is an example of how you can build a basic Pandas Series from a list:
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:
From the leagues series display the initial three items:
You can give names to your custom labels through the index argument.
You can make your personal labels as follows:
With the optional argument index customize your own label:
Through your custom-made labels, you can locate items by identifying them on their labels.
Display the value of index “a”:
Show the palindromes at “p2”, “p3” and “p5” locations:
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:
The following is an example of how you can generate a basic Pandas Series from a dictionary:
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:
In the following leagues dictionary generate a series whose data is only from the below indexes.
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”:
Make a data set of leagues_data by implementing the DataFrame() function:
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.