Create Pandas Series

Create Pandas Series

Pandas is a powerful data manipulation library in Python that offers a variety of functions to handle and analyze data efficiently. One of the fundamental data structures in Pandas is the Series. A Series is a one-dimensional array-like object capable of holding any data type (integers, strings, floating points, Python objects, etc.). It has an associated array of data labels, called its index. This article will explore how to create and manipulate Pandas Series with detailed examples.

Introduction to Pandas Series

A Pandas Series can be thought of as a column in a table. It is a one-dimensional array holding data of any type. The axis labels are collectively referred to as the index. The basic method to create a Series is to use:

import pandas as pd
s = pd.Series(data, index=index)

where data can be many different things:

  • a Python dict
  • an ndarray
  • a scalar value (like 5)

The passed index is a list of axis labels. Thus, it separates into a few cases depending on what data is.

Creating Series from Lists

One of the most common data types that is used to create a Series is a list.

Example 1: Basic Series from List

import pandas as pd

data = [1, 2, 3, 4, 5]
series = pd.Series(data)
print(series)

Output:

Create Pandas Series

Example 2: Series with Custom Index

import pandas as pd

data = [1, 2, 3, 4, 5]
index = ['a', 'b', 'c', 'd', 'e']
series = pd.Series(data, index=index)
print(series)

Output:

Create Pandas Series

Creating Series from Dictionary

When data is a dictionary, and an index is not passed, the Series index will be ordered by the dictionary’s insertion order.

Example 3: Series from Dictionary

import pandas as pd

data = {'a': 1, 'b': 2, 'c': 3}
series = pd.Series(data)
print(series)

Output:

Create Pandas Series

Example 4: Series from Dictionary with Index

import pandas as pd

data = {'a': 1, 'b': 2, 'c': 3}
index = ['c', 'a', 'b', 'd']
series = pd.Series(data, index=index)
print(series)

Output:

Create Pandas Series

Creating Series from Scalar Value

If data is a scalar value, an index must be provided. The value will be repeated to match the length of index.

Example 5: Scalar Series

import pandas as pd

data = 'pandasdataframe.com'
index = [0, 1, 2, 3]
series = pd.Series(data, index=index)
print(series)

Output:

Create Pandas Series

Data Selection in Series

Series provide array-like functionality and can be sliced and indexed like arrays.

Example 6: Selecting Data from Series

import pandas as pd

data = ['pandasdataframe.com', 'example', 'test', 'data']
series = pd.Series(data)
print(series[0])

Output:

Create Pandas Series

Example 7: Slicing Series

import pandas as pd

data = ['pandasdataframe.com', 'example', 'test', 'data']
series = pd.Series(data)
print(series[:2])

Output:

Create Pandas Series

Series Operations

You can perform a variety of operations on Series, such as mathematical operations, aggregations, and more.

Example 8: Mathematical Operations on Series

import pandas as pd

data = [1, 2, 3, 4]
series = pd.Series(data)
result = series + 10
print(result)

Output:

Create Pandas Series

Example 9: Aggregation Operations on Series

import pandas as pd

data = [1, 2, 3, 4]
series = pd.Series(data)
sum_result = series.sum()
print(sum_result)

Output:

Create Pandas Series

Handling Missing Data

Pandas provides functionalities to handle missing data in Series.

Example 10: Handling Missing Data

import pandas as pd

data = [1, None, 3, 4]
series = pd.Series(data)
cleaned_series = series.dropna()
print(cleaned_series)

Output:

Create Pandas Series

Create Pandas Series Conclusion

This article has introduced the basics of creating and manipulating Pandas Series. We’ve covered how to create Series from various data types, how to select and manipulate data within a Series, and how to handle missing data. Pandas Series is a versatile tool that can be used for a wide range of data analysis tasks.

Like(0)