Create a Pandas Series from Array (original) (raw)
Last Updated : 17 Mar, 2025
A **Pandas Series is a one-dimensional labeled array that stores various data types, including numbers (integers or floats), strings, and Python objects. It is a fundamental data structure in the Pandas library used for efficient data manipulation and analysis. In this guide we will explore two simple methods to create a Pandas **Series from a NumPy array.
Creating a Pandas Series Without an Index
By default when you create a Series from a NumPy array Pandas automatically assigns a numeric index starting from **0. Here pd.Series(data)
converts the array into a **Pandas Series automatically assigning an index.
Python `
import pandas as pd import numpy as np
data = np.array(['a', 'b', 'c', 'd', 'e'])
s = pd.Series(data) print(s)
`
**Output:
**Explanation:
- The default index starts from **0 and increments by **1.
- The data type (
dtype: object
) means it stores text values
Creating a Pandas Series With a Custom Index
In this method we specify custom indexes instead of using Pandas’ default numerical indexing. This is useful when working with structured data, such as **employee IDs, timestamps, or product codes where meaningful indexes enhance data retrieval and analysis.
Python `
import pandas as pd import numpy as np
data = np.array(['a', 'b', 'c', 'd', 'e'])
s = pd.Series(data, index=[1000, 1001, 1002, 1003, 1004])
print(s)
`
**Output:
**Explanation:
- Custom indexes (**1000, 1001, 1002…) replace the default ones and allow meaningful data representation.
- Custom indexing enhances **data retrieval, and make easier to access specific values directly using meaningful labels (e.g.,
s[1002]
instead ofs[2]
).
Creating a Pandas Series from a NumPy array is simple and efficient. You can use the default index for quick access or assign a custom index for better data organization.
Similar Reads
- Pandas Exercises and Programs Pandas is an open-source Python Library that is made mainly for working with relational or labelled data both easily and intuitively. This Python library is built on top of the NumPy library, providing various operations and data structures for manipulating numerical data and time series. Pandas is 6 min read
- Different ways to create Pandas Dataframe It is the most commonly used Pandas object. The pd.DataFrame() function is used to create a DataFrame in Pandas. There are several ways to create a Pandas Dataframe in Python. Example: Creating a DataFrame from a Dictionary [GFGTABS] Python import pandas as pd # initialize data of lists. data = { 7 min read