DEV Community

Cover image for Understanding Pandas Series Labeled and Unlabeled Data Structures
orioninsist
orioninsist

Posted on • Updated on

Understanding Pandas Series Labeled and Unlabeled Data Structures

Learn how to create and manipulate pandas Series, versatile data structures for efficient data handling in Python. Explore labeled and unlabeled formats, perform operations, and level up your data analysis skills! 🐼📊 #Python #Pandas #DataScience

Introduction

Welcome, dear readers! In this blog post, we will explore an essential building block of data analysis in Python - the "pandas.Series" data structure. With "pandas.Series," you can efficiently store, manipulate, and analyze data, both with labeled and unlabeled formats. We'll dive into the step-by-step process of creating and using pandas Series, followed by practical examples.

Step-by-Step Guide to Pandas Series

Importing Libraries and Initializing Data

import numpy as np
import pandas as pd

# Let's start with a dictionary and create a pandas Series from it:
myDictionary = {"Orion": 50, "Insist": 40, "Thinkpad": 30}
pd.Series(myDictionary)

Enter fullscreen mode Exit fullscreen mode

Here, we import the necessary libraries, numpy and pandas. We initialize data as a Python dictionary containing items and their corresponding values. Next, we create a pandas Series using the "pd.Series()" function.

Creating Series from Lists and Arrays

myAges = [50, 40, 30]
myNames = ["Orion", "Insist", "Thinkpad"]

# We can create a Series from a list directly:
pd.Series(myAges)

# Or, by pairing the list with another list to set custom labels:
pd.Series(myAges, myNames)

# Alternatively, we can set the index explicitly using the "index" parameter:
pd.Series(data=myAges, index=myNames)

Enter fullscreen mode Exit fullscreen mode

In this section, we demonstrate how to create Series from lists and arrays. We initially create a Series directly from the "myAges" list. Then, we create another Series by pairing "myAges" with "myNames" to set custom labels. Finally, we create a Series by explicitly specifying the index using the "index" parameter.

Using Numpy Arrays to Create Series

numpyArray = np.array([50, 40, 30])

# We can create a Series directly from a Numpy array:
pd.Series(numpyArray)

# Or, by setting custom labels using the "myNames" list:
pd.Series(numpyArray, myNames)

Enter fullscreen mode Exit fullscreen mode

In this part, we demonstrate how to create Series from Numpy arrays. We directly create a Series from the "numpyArray" using the "pd.Series()" function. Additionally, we show how to set custom labels using the "myNames" list.

Indexing and Operating on Series


# Series can have custom indices assigned to each value:
pd.Series(["Orion", "Insist", "Thinkpad"], [1, 2, 3])

# We can perform operations on Series:
competitionResult1 = pd.Series([10, 5, 1], ["Orion", "Insist", "Thinkpad"])
competitionResult2 = pd.Series([20, 10, 8], ["Orion", "Insist", "Thinkpad"])
competitionResult2["Insist"]
lastResult = competitionResult1 + competitionResult2

Enter fullscreen mode Exit fullscreen mode

In this section, we illustrate how to use custom indices in Series. We create a Series with custom indices assigned to each value.

Furthermore, we demonstrate the operations that can be performed on Series. We first create two Series, "competitionResult1" and "competitionResult2," representing the scores for different competitors. We then access the value associated with the label "Insist" in "competitionResult2" using indexing. Finally, we perform addition on the two Series, "competitionResult1" and "competitionResult2," to get the "lastResult."

Performing Operations on Different Series


differentSeries = pd.Series([20, 30, 40, 50], ["a", "b", "c", "d"])
differentSeries2 = pd.Series([10, 5, 3, 1], ["a", "c", "f", "g"])
differentSeries + differentSeries2

Enter fullscreen mode Exit fullscreen mode

In this section, we showcase how to perform operations on different Series. We create two Series, "differentSeries" and "differentSeries2," with distinct indices. Then, we perform addition on these Series. Note that the resulting Series will contain NaN (Not a Number) for the non-overlapping indices.

Project Purpose and Conclusion

The main aim of this project was to understand the concept of pandas Series and explore how they can be created, manipulated, and combined. We learned that pandas Series are incredibly versatile and allow for easy data handling with labeled or unlabeled formats. With the ability to perform operations on Series, data analysis becomes more efficient and convenient.

In conclusion, pandas Series play a crucial role in data analysis with Python, providing a powerful data structure to work with various datasets. As you delve deeper into the world of data science and analysis, mastering pandas Series will prove to be an invaluable skill.

GitHub

Thank you

Thank you for your continued support, and I’m excited to have you on this enriching journey!

Sincerely, Founder of orioninsist

Follow the white rabbit
Thank you for your support! Hello friends! I want to express my gratitude for your support. Your interest and encouragement mean a lot to me. To keep our connection strong and to provide you with more valuable content, I encourage you to stay connected with me on my social media platforms.

I am excited to share more content with you through these platforms and I value your engagement and feedback. Thank you once again for your support. Let’s stay connected and keep the conversation going!

Your feedback and engagement mean the world to me. Thank you once again for your unwavering support.

Let’s continue to “follow the white rabbit” and discover new horizons together!

Best regards,
Murat Kurkoglu
Founder of orioninsist
Google Survey Forms

Top comments (0)