4 C
London
Friday, December 20, 2024
HomePandas in PythonDataFrame Functions in PythonPandas: How to Convert Specific Columns to NumPy Array

Pandas: How to Convert Specific Columns to NumPy Array

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following methods to convert specific columns in a pandas DataFrame to a NumPy array:

Method 1: Convert One Column to NumPy Array

column_to_numpy = df['col1'].to_numpy()

Method 2: Convert Multiple Columns to NumPy Array

columns_to_numpy = df[['col1', 'col3', 'col4']].to_numpy()

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

  team  points  assists  rebounds
0    A      18        5        11
1    B      22        7         8
2    C      19        7        10
3    D      14        9         6
4    E      14       12         6
5    F      11        9         5
6    G      20        9         9
7    H      28        4        12

Example 1: Convert One Column to NumPy Array

The following code shows how to convert the points column in the DataFrame to a NumPy array:

#convert points column to NumPy array
column_to_numpy = df['points'].to_numpy()

#view result
print(column_to_numpy)

[18 22 19 14 14 11 20 28]

We can confirm that the result is indeed a NumPy array by using the type() function:

#view data type
print(type(column_to_numpy))


Example 2: Convert Multiple Columns to NumPy Array

The following code shows how to convert the team and assists columns in the DataFrame to a multidimensional NumPy array:

#convert team and assists columns to NumPy array
columns_to_numpy = df[['team', 'assists']].to_numpy()

#view result
print(columns_to_numpy)

[['A' 5]
 ['B' 7]
 ['C' 7]
 ['D' 9]
 ['E' 12]
 ['F' 9]
 ['G' 9]
 ['H' 4]]

We can confirm that the result is indeed a NumPy array by using the type() function:

#view data type
print(type(columns_to_numpy))


We can also use the shape function to view the shape of the resulting NumPy array:

#view shape of array
print(columns_to_numpy.shape)

(8, 2)

We can see that the resulting NumPy array has 8 rows and 2 columns.

Additional Resources

The following tutorials explain how to perform other common tasks in NumPy:

How to Remove Specific Elements from NumPy Array
How to Replace Elements in NumPy Array
How to Get Specific Row from NumPy Array

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories