Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Pandas Series Object – A Helpful Guide with Examples
1
Pandas Series Object – A Helpful Guide with Examples

5/5 – (1 vote)

If you’re working with data in Python, you might have come across the pandas library. [Image: 1f43c.png]

One of the key components of pandas is the Series object, which is a one-dimensional, labeled array capable of holding data of any type, such as integers, strings, floats, and even Python objects [Image: 1f603.png].

The Series object serves as a foundation for organizing and manipulating data within the pandas library.

This article will teach you more about this crucial data structure and how it can benefit your data analysis workflows. Let’s get started! [Image: 1f447.png]

Creating a Pandas Series

[Image: image-139-1024x682.png]

In this section, you’ll learn how to create a Pandas Series, a powerful one-dimensional labeled array capable of holding any data type.

To create a Series, you can use the
Code:
Series()
constructor from the Pandas library.

Make sure you have Pandas installed and imported:

Code:
import pandas as pd

Now, you can create a Series using the
Code:
pd.Series()
function, and pass in various data structures like lists, dictionaries, or even scalar values. For example:

Code:
my_list = [1, 2, 3, 4] my_series = pd.Series(my_list)

The
Code:
Series()
constructor accepts various parameters that help you customize the resulting series, including:
  • Code:
    data
    : This is the input data—arrays, dicts, or scalars.

  • Code:
    index
    : You can provide a custom index for your series to label the values. If you don’t supply one, Pandas will automatically create an integer index (0, 1, 2…).

Here’s an example of creating a Series with a custom index:

Code:
custom_index = ['a', 'b', 'c', 'd'] my_series = pd.Series(my_list, index=custom_index)

When you create a Series object with a dictionary, Pandas automatically takes the keys as the index and the values as the series data:

Code:
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4} my_series = pd.Series(my_dict)

[Image: 1f4a1.png] Remember: Your Series can hold various data types, including strings, numbers, and even objects.

Pandas Series Indexing

[Image: image-142.png]

Next, you’ll learn the best ways to index and select data from a Pandas Series, making your data analysis tasks more manageable and enjoyable.

Again, a Pandas Series is a one-dimensional labeled array, and it can hold various data types like integers, floats, and strings. The series object contains an index, which serves multiple purposes, such as metadata identification, automatic and explicit data alignment, and intuitive data retrieval and modification [Image: 1f6e0.png].

There are two types of indexing available in a Pandas Series:

  1. Position-based indexing – this uses integer positions to access data. The pandas function
    Code:
    iloc[]
    comes in handy for this purpose.

  2. Label-based indexing – this uses index labels for data access. The pandas function
    Code:
    loc[]
    works great for this type of indexing.

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

[Image: 1f4a1.png] Recommended: Pandas
Code:
loc()
and
Code:
iloc()
– A Simple Guide with Video


Let’s examine some examples of indexing and selection in a Pandas Series:

Code:
import pandas as pd # Sample Pandas Series data = pd.Series([10, 20, 30, 40, 50], index=['a', 'b', 'c', 'd', 'e']) # Position-based indexing (using iloc) position_index = data.iloc[2] # Retrieves the value at position 2 (output: 30) # Label-based indexing (using loc) label_index = data.loc['b'] # Retrieves the value with the label 'b' (output: 20)

Keep in mind that while working with Pandas Series, the index labels do not have to be unique but must be hashable types. This means they should be of immutable data types like strings, numbers, or tuples [Image: 1f31f.png].

[Image: 1f4a1.png] Recommended: Mutable vs. Immutable Objects in Python

Accessing Values in a Pandas Series

[Image: image-146-1024x682.png]

So you’re working with Pandas Series and want to access their values. I already showed you this in the previous section but let’s repeat this once again. Repetition. Repetition. Repetition!

First of all, create your Pandas Series:

Code:
import pandas as pd data = ['A', 'B', 'C', 'D', 'E'] my_series = pd.Series(data)

Now that you have your Series, let’s talk about accessing its values [Image: 1f680.png]:

  1. Using index: You can access an element in a Series using its index, just like you do with lists:

Code:
third_value = my_series[2] print(third_value) # Output: C
  1. Using
    Code:
    .loc[]
    : Access an element using its index label with the
    Code:
    .loc[]
    accessor, which is useful when you have custom index names[Image: 1f516.png]:

Code:
data = ['A', 'B', 'C', 'D', 'E'] index_labels = ['one', 'two', 'three', 'four', 'five'] my_series = pd.Series(data, index=index_labels) second_value = my_series.loc['two'] print(second_value) # Output: B
  1. Using
    Code:
    .iloc[]
    : Access a value based on its integer position with the
    Code:
    .iloc[]
    accessor. This is particularly helpful when you have non-integer index labels[Image: 1f3af.png]:

Code:
value_at_position_3 = my_series.iloc[2] print(value_at_position_3) # Output: C

Iterating through a Pandas Series

[Image: image-143.png]

[Image: 1f4a1.png] Although iterating over a Series is possible, it’s generally discouraged in the Pandas community due to its suboptimal performance. Instead, try using vectorization or other optimized methods, such as
Code:
apply
,
Code:
transform
, or
Code:
agg
.

This section will discuss Series iteration methods, but always remember to consider potential alternatives first!

When you absolutely need to iterate through a Series, you can use the
Code:
iteritems()
function, which returns an iterator of index-value pairs. Here’s an example:

Code:
for idx, val in your_series.iteritems(): # Do something with idx and val

Another method to iterate over a Pandas Series is by converting it into a list using the
Code:
tolist()
function, like this:

Code:
for val in your_series.tolist(): # Do something with val

[Image: 1f680.png] However, keep in mind that these approaches are suboptimal and should be avoided whenever possible. Instead, try one of the following efficient techniques:
  • Vectorized operations: Apply arithmetic or comparison operations directly on the Series.

  • Use
    Code:
    apply()
    : Apply a custom function element-wise.

  • Use
    Code:
    agg()
    : Aggregate multiple operations to be applied.

  • Use
    Code:
    transform()
    : Apply a function and return a similarly-sized Series.

Sorting a Pandas Series [Image: 1f504.png]

[Image: image-141-1024x804.png]

Sorting a Pandas Series is pretty straightforward. With the
Code:
sort_values()
function, you can easily reorder your series, either in ascending or descending order.

First, you must import the Pandas library and create a Pandas Series:

Code:
import pandas as pd s = pd.Series([100, 200, 54.67, 300.12, 400])

To sort the values in the series, just use the
Code:
sort_values()
function like this:

Code:
sorted_series = s.sort_values()

By default, the values will be sorted in ascending order. If you want to sort them in descending order, just set the
Code:
ascending
parameter to
Code:
False
:

Code:
sorted_series = s.sort_values(ascending=False)

You can also control the sorting method using the
Code:
kind
parameter. Supported options are
Code:
'quicksort'
,
Code:
'mergesort'
, and
Code:
'heapsort'
. For example:

Code:
sorted_series = s.sort_values(kind='mergesort')

When dealing with missing values (
Code:
NaN
) in your series, you can use the
Code:
na_position
parameter to specify their position in the sorted series. The default value is
Code:
'last'
, which places missing values at the end.

To put them at the beginning of the sorted series, just set the
Code:
na_position
parameter to
Code:
'first'
:

Code:
sorted_series = s.sort_values(na_position='first')
Applying Functions to a Pandas Series

You might come across situations where you want to apply a custom function to your Pandas Series. Let’s dive into how you can do that using the
Code:
apply()
method. [Image: 1f680.png]

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

To begin with, the
Code:
apply()
method is quite flexible and allows you to apply a wide range of functions on your Series. These functions could be NumPy’s universal functions (
Code:
ufuncs
), built-in Python functions, or user-defined functions. Regardless of the type,
Code:
apply()
will work like magic.[Image: 1f3a9.png][Image: 2728.png]

For instance, let’s say you have a Pandas Series containing square numbers, and you want to find the square root of these numbers:

Code:
import pandas as pd square_numbers = pd.Series([4, 9, 16, 25, 36])

Now, you can use the
Code:
apply()
method along with the built-in Python function
Code:
sqrt()
to calculate the square root:

Code:
import math square_roots = square_numbers.apply(math.sqrt) print(square_roots)

You’ll get the following output:

Code:
0 2.0 1 3.0 2 4.0 3 5.0 4 6.0 dtype: float64

Great job! [Image: 1f389.png] Now, let’s consider you want to create your own function to check if the numbers in a Series are even. Here’s how you can achieve that:

Code:
def is_even(number): return number % 2 == 0 even_numbers = square_numbers.apply(is_even) print(even_numbers)

And the output would look like this:

Code:
0 True 1 False 2 True 3 False 4 True dtype: bool

Congratulations! [Image: 1f973.png] You’ve successfully used the
Code:
apply()
method with a custom function.

Replacing Values in a Pandas Series

[Image: image-145-1024x682.png]

You might want to replace specific values within a Pandas Series to clean up your data or transform it into a more meaningful format. The
Code:
replace()
function is here to help you do that! [Image: 1f603.png]

How to use
Code:
replace()


To use the
Code:
replace()
function, simply call it on your Series object like this:
Code:
your_series.replace(to_replace, value)
.
Code:
to_replace
is the value you want to replace, and
Code:
value
is the new value you want to insert instead. You can also use regex for more advanced replacements.

Let’s see an example:

Code:
import pandas as pd data = pd.Series([1, 2, 3, 4]) data = data.replace(2, "Two") print(data)

This code will replace the value
Code:
2
with the string
Code:
"Two"
in your Series. [Image: 1f504.png]

Multiple replacements

You can replace multiple values simultaneously by passing a dictionary or two lists to the function. For example:

Code:
data = pd.Series([1, 2, 3, 4]) data = data.replace({1: 'One', 4: 'Four'}) print(data)

In this case,
Code:
1
will be replaced with
Code:
'One'
and
Code:
4
with
Code:
'Four'
. [Image: 1f389.png]

Limiting replacements

You can limit the number of replacements by providing the
Code:
limit
parameter. For example, if you set
Code:
limit=1
, only the first occurrence of the value will be replaced.

Code:
data = pd.Series([2, 2, 2, 2]) data = data.replace(2, "Two", limit=1) print(data)

This code will replace only the first occurrence of
Code:
2
with
Code:
"Two"
in the Series. [Image: 2728.png]

Appending and Concatenating Pandas Series

You might want to combine your pandas Series while working with your data. Worry not! [Image: 1f603.png] Pandas provides easy and convenient ways to append and concatenate your Series.

Appending Series

Appending Series can be done using the
Code:
append()
method. It allows you to concatenate two or more Series objects. To use it, simply call the method on one series and pass the other series as the argument.

For example:

Code:
import pandas as pd series1 = pd.Series([1, 2, 3]) series2 = pd.Series([4, 5, 6]) result = series1.append(series2) print(result)

Output:

Code:
0 1 1 2 2 3 0 4 1 5 2 6 dtype: int64

However, appending Series iteratively may become computationally expensive. In such cases, consider using
Code:
concat()
instead. [Image: 1f447.png]

Concatenating Series

The
Code:
concat()
function is more efficient when you need to combine multiple Series vertically. Simply provide a list of Series you want to concatenate as its argument, like so:

Code:
import pandas as pd series_list = [ pd.Series(range(1, 6), index=list('abcde')), pd.Series(range(1, 6), index=list('fghij')), pd.Series(range(1, 6), index=list('klmno')) ] combined_series = pd.concat(series_list) print(combined_series)

Output:

Code:
a 1 b 2 c 3 d 4 e 5 f 1 g 2 h 3 i 4 j 5 k 1 l 2 m 3 n 4 o 5 dtype: int64

[Image: 1f680.png] There you have it! You’ve combined your Pandas Series using
Code:
append()
and
Code:
concat()
.

Renaming a Pandas Series

[Image: image-340.png]

Renaming a Pandas Series is a simple yet useful operation you may need in your data analysis process.

To start, the
Code:
rename()
method in Pandas can be used to alter the index labels or name of a given Series object. But, if you just want to change the name of the Series, you can set the
Code:
name
attribute directly. For instance, if you have a Series object called
Code:
my_series
, you can rename it to
Code:
"New_Name"
like this:

Code:
my_series.name = "New_Name"

Now, let’s say you want to rename the index labels of your Series. You can do this using the
Code:
rename()
method. Here’s an example:

Code:
renamed_series = my_series.rename(index={"old_label1": "new_label1", "old_label2": "new_label2"})

The
Code:
rename()
method also accepts functions for more complex transformations. For example, if you want to capitalize all index labels, you can do it like this:

Code:
capitalized_series = my_series.rename(index=lambda x: x.capitalize())

Keep in mind that the
Code:
rename()
method creates a new Series by default and doesn’t modify the original one. If you want to change the original Series in-place, just set the
Code:
inplace
argument to
Code:
True
:

Code:
my_series.rename(index={"old_label1": "new_label1", "old_label2": "new_label2"}, inplace=True)
Unique Values in a Pandas Series

[Image: image-341.png]

To find unique values in a Pandas Series, you can use the
Code:
unique()
method[Image: 1f50d.png]. This method returns the unique values in the series without sorting them, maintaining the order of appearance.

Here’s a quick example:

Code:
import pandas as pd data = {'A': [1, 2, 1, 4, 5, 4]} series = pd.Series(data['A']) unique_values = series.unique() print(unique_values)

The output will be:
Code:
[1, 2, 4, 5]

When working with missing values, keep in mind that the
Code:
unique()
method includes NaN values if they exist in the series. This behavior ensures you are aware of missing data in your dataset [Image: 1f4da.png].

If you need to find unique values in multiple columns, the
Code:
unique()
method might not be the best choice, as it only works with Series objects, not DataFrames. Instead, use the
Code:
.drop_duplicates()
method to get unique combinations of multiple columns.

[Image: 1f4a1.png] Recommended: The Ultimate Guide to Data Cleaning in Python and Pandas

To summarize, when finding unique values in a Pandas Series:
  • Use the
    Code:
    unique()
    method for a single column [Image: 1f9ea.png]

  • Remember that
    Code:
    NaN
    values will be included as unique values when present [Image: 1f4cc.png]

  • Use the
    Code:
    .drop_duplicates()
    method for multiple columns when needed [Image: 1f504.png]

With these tips, you’re ready to efficiently handle unique values in your Pandas data analysis! [Image: 1f43c.png][Image: 1f4bb.png]

Converting Pandas Series to Different Data Types

[Image: image-342.png]

You can convert a Pandas Series to different data types to modify your data and simplify your work. In this section, you’ll learn how to transform a Series into a DataFrame, List, Dictionary, Array, String, and Numpy Array. Let’s dive in! [Image: 1f680.png]

Series to DataFrame

To convert a Series to a DataFrame, use the
Code:
to_frame()
method. Here’s how:

Code:
import pandas as pd data = pd.Series([1, 2, 3, 4]) df = data.to_frame() print(df)

This code will output:

Code:
0 0 1 1 2 2 3 3 4
Series to List

For transforming a Series to a List, simply call the
Code:
tolist()
method, like this:

Code:
data_list = data.tolist() print(data_list)

Output:

Code:
[1, 2, 3, 4]
Series to Dictionary

To convert your Series into a Dictionary, use the
Code:
to_dict()
method:

Code:
data_dict = data.to_dict() print(data_dict)

This results in:

Code:
{0: 1, 1: 2, 2: 3, 3: 4}

The keys are now indexes, and the values are the original Series data.

Series to Array

Convert your Series to an Array by accessing its
Code:
.array
attribute:

Code:
data_array = data.array print(data_array)

Output:

Code:
<PandasArray> [1, 2, 3, 4]
Series to String

To join all elements of a Series into a single String, use the
Code:
join()
function from the
Code:
str
library:

Code:
data_str = ''.join(map(str, data)) print(data_str)

This will result in:

Code:
1234
Series to Numpy Array

For converting a Series into a Numpy Array, call the
Code:
to_numpy()
method:

Code:
import numpy as np data_numpy = data.to_numpy() print(data_numpy)

Output:

Code:
array([1, 2, 3, 4], dtype=int64)

Now you’re all set to manipulate your Pandas Series objects and adapt them to different data types! [Image: 1f389.png]

Python Pandas Series in Practice [Image: 1f43c.png][Image: 1f4bb.png]

[Image: image-343.png]

A Pandas Series is a one-dimensional array-like object that’s capable of holding any data type. It’s one of the essential data structures in the Pandas library, along with the DataFrame. Series is an easy way to organize and manipulate your data, especially when dealing with labeled data, such as SQL databases or dictionary keys. [Image: 1f511.png][Image: 26a1.png]

To begin, import the Pandas library, which is usually done with the alias ‘
Code:
pd
‘:

Code:
import pandas as pd
Creating a Pandas Series [Image: 1f4dd.png][Image: 1f528.png]

To create a Series, simply pass a list, ndarray, or dictionary to the
Code:
pd.Series()
function. For example, you can create a Series with integers:

Code:
integer_series = pd.Series([1, 2, 3, 4, 5])

Or with strings:

Code:
string_series = pd.Series(['apple', 'banana', 'cherry'])

In case you want your Series to have an explicit index, you can specify the
Code:
index
parameter:

Code:
indexed_series = pd.Series(['apple', 'banana', 'cherry'], index=['a', 'b', 'c'])
Accessing and Manipulating Series Data [Image: 1f6aa.png][Image: 1f527.png]

Now that you have your Series, here’s how you can access and manipulate the data:
  • Accessing data by index (using both implicit and explicit index):
    • First item:
      Code:
      integer_series[0]
      or
      Code:
      indexed_series['a']

    • Slicing:
      Code:
      integer_series[1:3]

  • Adding new data:
    • Append:
      Code:
      string_series.append(pd.Series(['date']))

    • Add with a label:
      Code:
      indexed_series['d'] = 'date'

  • Common Series methods:
    • Code:
      all()
      – Check if all elements are true

    • Code:
      any()
      – Check if any elements are true

    • Code:
      unique()
      – Get unique values

    • Code:
      ge(another_series)
      – Compare elements element-wise with another Series

These are just a few examples of interacting with a Pandas Series. There are many other functionalities you can explore!

Practice makes perfect, so feel free to join our free email academy where I’ll show you practical coding projects, data science, exponential technologies in AI and blockchain engineering, Python, and much more. How can you join? Simply download your free cheat sheets by entering your name here:

Let your creativity run wild and happy coding! [Image: 1f917.png][Image: 1f4a1.png]
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: