search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | to_numpy method

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas DataFrame.to_numpy(~) method returns the values of the DataFrame as a 2D NumPy array.

Parameters

1. dtypelink | string or type | optional

The desired data type of the returned NumPy array. By default, the data type will be the common type of the array's values. See examples below for clarification.

2. copylink | boolean | optional

  • If True, then a new NumPy array is created. Modifying this array would not affect the source DataFrame and vice versa.

  • If False, then a reference to the DataFrame's NumPy array representation is returned. This means that if you modify the array, then the original DataFrame will also be modified, and vice versa.

By default copy=False.

Return Value

A Numpy array holding all the values of the source DataFrame.

Examples

Obtaining the NumPy Array representation

Consider the following DataFrame:

df = pd.DataFrame({"A":[1,2], "B":[3,4]})
df
   A  B
0  1  3
1  2  4

To get the values of df as a NumPy array:

df.to_numpy()
array([[1, 3],
       [2, 4]])

Data type of returned NumPy array

Consider the following DataFrame:

df = pd.DataFrame({"A":[1,2], "B":[3.0,4.0]})
df
A B
0  1  3.0
1  2  4.0

Here, column A is of type int, while column B is of type float.

The limitation with NumPy arrays is that all their values must be of one type. Since our df has two types, the to_numpy(~) method will opt to use float as int can be represented using float:

df.to_numpy().dtype
dtype('float64')

Creating a new copy

Consider the following DataFrame:

df = pd.DataFrame({"A":[1,2], "B":[3,4]})
df
A B
0  1  3
1  2  4

To create a new NumPy array, set copy=True. In the code snippet below, we modify the first value of the array and check to see whether the source DataFrame, df, has been modified:

arr = df.to_numpy(copy=True)
arr[0,0] = 5
df
   A  B
0  1  3
1  2  4

Notice how the first value of the DataFrame (1) is left intact since arr is a copy of df.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!