Pandas Series

A Pandas Series is a one-dimensional labeled array-like object that can hold data of any type.

A Pandas Series can be thought of as a column in a spreadsheet or a single column of a DataFrame. It consists of two main components: the labels and the data.

For example,

0    'John'
1    30
2    6.2
3    False
dtype: object

Here, the series has two columns, labels (0, 1, 2 and 3) and data ('John', 30, 6.2, False).

The labels are the index values assigned to each data point, while the data represents the actual values stored in the Series.

Note: Pandas Series can store elements of different data types. It uses a concept called dtype (data type) to manage and represent the underlying data in a Series.

By default, Pandas internally represents the Series of different datatypes using the object data type, a general-purpose data type that can hold any text or numeric data type(strings, integers, booleans, etc).


Create a Pandas Series

There are multiple ways to create a Pandas Series, but the most common way is by using a Python list. Let’s see an example of creating a Series using a list:

import pandas as pd

# create a list
data = [10, 20, 30, 40, 50]

# create a series from the list
my_series = pd.Series(data)

print(my_series)

Output

0    10
1    20
2    30
3    40
4    50
dtype: int64

In this example, we created a Python list called data containing five integer values. We then passed this list to the Series() function, which converted it into a Pandas Series called my_series.

Here, dtype: int64 denotes that the series stores the values of int64 types.


Labels

The labels in the Pandas Series are index numbers by default. Like in dataframe and array, the index number in series starts from 0.

Such labels can be used to access a specified value. For example,

import pandas as pd

# create a list
data = [10, 20, 30, 40, 50]

# create a series from the list
my_series = pd.Series(data)

# display third value in the series
print(my_series[2])

Output

30

Here, we accessed the third element of my_series using a label.

We can also specify labels while creating the series using the index argument in the Series() method. For example,

import pandas as pd 

# create a list
a = [1, 3, 5]

# create a series and specify labels
my_series = pd.Series(a, index = ["x", "y", "z"])

print(my_series)

Output

x    1
y    3
z    5
dtype: int64

In this example,we passed index = ["x", "y", "z"] as an argument to Series() to specify the labels explicitly.

To access the series elements, we use the specified labels instead of the default index number. For example,

import pandas as pd 

# create a list
a = [1, 3, 5]

# create a series and specify labels
my_series = pd.Series(a, index = ["x", "y", "z"])

# display the value with label y
print(my_series["y"])

Output

3

Create Series From a Python Dictionary

You can also create a Pandas Series from a Python dictionary. For example,

import pandas as pd

# create a dictionary
grades = {"Semester1": 3.25, "Semester2": 3.28, "Semester3": 3.75}

# create a series from the dictionary
my_series = pd.Series(grades)

# display the series
print(my_series)

Output

Semester1    3.25
Semester2    3.28
Semester3    3.75
dtype: float64

Here, we created a series named my_series of type float64 with a dictionary named grades.

Notice that the keys of the dictionary have become the labels.

We can further customize the series by selecting specific items from the dictionary using the index argument. For example,

import pandas as pd

# create a dictionary
grades = {"Semester1": 3.25, "Semester2": 3.28, "Semester3": 3.75}

# select specific dictionary items using index argument
my_series = pd.Series(grades, index = ["Semester1", "Semester2"])

# display the series
print(my_series)

Output

Semester1    3.25
Semester2    3.28
dtype: float64
© 2024 All rights reserved. | Made With 🤍 By The_MAK Team