In this tutorial, we will look at how to convert a pandas series to a NumPy array.
How to convert a pandas series to an array?
There are a number of ways to get an array from a pandas series. You can directly get a numpy array of series values by accessing the .values
attribute of the series or you can use the pandas series to_numpy()
function. The following is the syntax to use the above methods:
# using .values arr = s.values # using to_numpy() arr = s.to_numpy()
Here, s is the pandas series you want to convert. Both the methods return a numpy array of the series values.
Examples
Let’s look at some examples of using the above methods to create a numpy array from a series. First, we’ll create a sample pandas series which we will be using throughout this tutorial.
import pandas as pd # pandas series Wimbledon winners from 2015 to 2019 wimbledon_winners = pd.Series(index=[2015, 2016, 2017, 2018, 2019], data=['Novak Djokovic', 'Andy Murray', 'Roger Federer', 'Novak Djokovic', 'Novak Djokovic'], name='Name') # display the series print(wimbledon_winners)
Output:
2015 Novak Djokovic 2016 Andy Murray 2017 Roger Federer 2018 Novak Djokovic 2019 Novak Djokovic Name: Name, dtype: object
You can see the contents of the series object above. Let’s confirm the type of the object.
# check the type print(type(wimbledon_winners))
Output:
<class 'pandas.core.series.Series'>
We now have a pandas series containing the name of Wimbledon Winners from 2015 to 2019 with the year as its index.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
1. Series to numpy array using .values
You can easily get a numpy array of series values by accessing the .values
attribute of the series. Let’s do that with our “wimbledon_winners” series created above.
arr = wimbledon_winners.values # check the type print(type(arr)) # print the content print(arr)
Output:
<class 'numpy.ndarray'> ['Novak Djokovic' 'Andy Murray' 'Roger Federer' 'Novak Djokovic' 'Novak Djokovic']
You can see that .values
gives a numpy.ndarray
object of the series values.
2. Series to numpy array using to_numpy()
Alternatively, you can use the pandas series to_numpy()
function to create a numpy array of series values. Let’s apply this function to the “wimbledon_winners” series.
arr = wimbledon_winners.to_numpy() # check the type print(type(arr)) # print the content print(arr)
Output:
<class 'numpy.ndarray'> ['Novak Djokovic' 'Andy Murray' 'Roger Federer' 'Novak Djokovic' 'Novak Djokovic']
You can see that the resulting numpy array has all the values from the series “wimbledon_winners”.
For more on the to_numpy() function, refer to its documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on pandas series –
- Convert Pandas Series to a DataFrame
- Convert Pandas Series to a List
- Convert Pandas Series to a NumPy Array
- Convert Pandas Series to a Dictionary
- Sort a Pandas Series
- Append Two Pandas Series
- Apply a Function to a Pandas Series
- Pandas – Shift column values up or down
- Plot a Histogram of Pandas Series Values
- Create a Pie Chart of Pandas Series Values
- Plot a Bar Chart of Pandas Series Values
- Create a Boxplot from Pandas Series Values
- Create a Density Plot from Pandas Series Values