Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert Pandas DataFrame/Series to NumPy Array?

#1
How to Convert Pandas DataFrame/Series to NumPy Array?

5/5 – (1 vote)

? Programming Challenge: Given a Pandas DataFrame or a Pandas Series object. How to convert them to a NumPy array?

How to Convert Pandas DataFrame/Series to NumPy Array?

In this short tutorial, you’ll learn (1) how to convert a 1D pandas Series to a NumPy array, and (2) how to convert a 2D pandas DataFrame to an array. Let’s get started with the first! ?

Convert Pandas Series to NumPy Array


First, let’s create a Pandas Series.

import pandas as pd # create dataframe df df = pd.Series([22,21,20,14], name= 'GSTitles', index= ['Nadal','Djokovic','Federer','Sampras'])
print(df)

Here’s the resulting Series df:

Nadal 22
Djokovic 21
Federer 20
Sampras 14
Name: GSTitles, dtype: int64

Now that we have our Pandas Series, you can convert this to a NumPy Array using the DataFrame.to_numpy() method.

Like so:

print(df.to_numpy())
# [22 21 20 14]

The resulting object is a NumPy array:

print(type(df.to_numpy()))
# <class 'numpy.ndarray'>

⚡ Attention: There is also the .values() method, but that is being deprecated now – when you look at the Pandas documentation, there is a warning “We recommend using DataFrame.to_numpy instead”.

With this method, only the values in the DataFrame or Series will return. The index labels will be removed.

Here’s how that’ll work:

print(df.values)
# [22 21 20 14]

This was a 1-dimensional array or a Series. Let’s move on to the 2D case next. ???

Convert DataFrame to NumPy Array


? Question: Let’s try with a two-dimensional DataFrame — how to convert it to a NumPy array?

First, let’s print the dimension of the previous Series to confirm that it was, indeed, a 1D data structure:

print(df.ndim)
# 1

Next, you create a 2D DataFrame object:

import pandas as pd # Create a 2D DataFrame object
df2 = pd.DataFrame(data={'Nadal': [2, 14, 2, 4], 'Djokovic': [9, 2, 7, 3], 'Federer': [6, 1, 8, 5], 'Sampras': [2, 0, 7, 5]}, index=['AO', 'F', 'W', 'US']) print(df2)

Here’s the resulting DataFrame:


Nadal Djokovic Federer Sampras
AO 2 9 6 2
F 14 2 1 0
W 2 7 8 7
US 4 3 5 5

Now, let’s dive into the conversion of this DataFrame to a NumPy array by using the DataFrame.to_numpy() method.

# Convert this DataFrame to a NumPy array
print(df2.to_numpy())

The output shows a NumPy array from the 2D DataFrame — great! ?

[[ 2 9 6 2] [14 2 1 0] [ 2 7 8 7] [ 4 3 5 5]]

You can see that all indexing metadata has been stripped away from the resulting NumPy array!

Convert Specific Columns from DataFrame to NumPy Array


You can also convert specific columns of a Pandas DataFrame by accessing the columns using pandas indexing and calling the .to_numpy() method on the resulting view object.

Here’s an example:

print(df2[['Djokovic', 'Federer']].to_numpy())

The output:

[[9 6] [2 1] [7 8] [3 5]]

Summary


You can convert a Pandas DataFrame or a Pandas Series object to a NumPy array by means of the df.to_numpy() method. The indexing metadata will be removed.

You can also convert specific columns of a Pandas DataFrame by accessing the columns using pandas indexing and calling the .to_numpy() method on the resulting view object.


Thanks for reading through the whole tutorial! ?



https://www.sickgaming.net/blog/2022/10/...mpy-array/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Making $65 per Hour on Upwork with Pandas xSicKxBot 0 1,320 05-24-2023, 08:16 PM
Last Post: xSicKxBot
  [Tut] Pandas Series Object – A Helpful Guide with Examples xSicKxBot 0 1,311 05-01-2023, 01:30 AM
Last Post: xSicKxBot
  [Tut] Python List of Tuples to DataFrame ? xSicKxBot 0 1,509 04-22-2023, 06:10 AM
Last Post: xSicKxBot
  [Tut] Dictionary of Lists to DataFrame – Python Conversion xSicKxBot 0 1,377 04-17-2023, 03:46 AM
Last Post: xSicKxBot
  [Tut] Pandas Boolean Indexing xSicKxBot 0 1,309 04-16-2023, 10:54 AM
Last Post: xSicKxBot
  [Tut] Python List of Dicts to Pandas DataFrame xSicKxBot 0 1,529 04-11-2023, 04:15 AM
Last Post: xSicKxBot
  [Tut] How to Create a DataFrame From Lists? xSicKxBot 0 1,223 12-17-2022, 03:17 PM
Last Post: xSicKxBot
  [Tut] Python Library Hijacking – A Simple Demonstration on NumPy xSicKxBot 0 1,250 11-22-2022, 01:48 AM
Last Post: xSicKxBot
  [Tut] How to Filter Data from an Excel File in Python with Pandas xSicKxBot 0 1,225 10-31-2022, 05:36 AM
Last Post: xSicKxBot
  [Tut] Solidity Bytes and String Arrays, Concat, Allocating Memory, and Array Literals xSicKxBot 0 1,201 10-25-2022, 09:13 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016