Python convert dataframe to numpy array
In this post, you will learn how to convert a dataframe to numpy array in Python programming.
A DataFrame is a tabular-like data structure containing an ordered collection of columns. The each column can be of different data types, like numeric, boolean, strings, etc. It has both a row and a column index. It is basically used for analytical purposes.
NumPy is the fundamental package for scientific computing with Python. It is a highly optimized library for numerical operations. The support of NumPy makes the task more easier. Several libraries like OpenCV, SciPy, Matplotlib when combine with NumPy increase the number of weapons in your arsenal.
We can easily convert Pandas DataFrame to numpy array by using the function DataFrame.to_numpy(). This function returns the numpy ndarray when applied on the DataFrame.
SyntaxDataFrame.to_numpy(dtype=None, copy=False)
Here, the first argument is the dtype to pass to numpy.asarray(). The second argument returns the boolean value that has the default value False. This function returns the numpy.ndarray as an output.
Convert the DataFrame to numpy array
Pandas is superb for taking care of your datasets, but you may discover it lacks in the statistical analysis power you need. This where it proves to be useful to convert your DataFrame to a NumPy Array.
import pandas as pd
# create a dataframe
df = pd.DataFrame(
[[5, 17, 10],
[15, 8, 4],
[7, 23, 18],
[23, 8, 11]],
columns=['x', 'y', 'z'])
print('DataFrame\n-------------\n', df)
# convert dataframe to numpy array
num_arr = df.to_numpy()
print('\nNumpy Array\n-------------\n', num_arr)
Output of the above code:
DataFrame
-------------
x y z
0 5 17 10
1 15 8 4
2 7 23 18
3 23 8 11
Numpy Array
-------------
[[ 5 17 10]
[15 8 4]
[ 7 23 18]
[23 8 11]]
Convert the DataFrame to numpy array with different datatype
Suppose we have a DataFrame with sections of different datatypes, then the returned NumPy Array comprises of elements of a single datatype. The lowest datatype of the DataFrame is considered for the datatype of the NumPy Array. The following example demonstrates this:
import pandas as pd
# create a dataframe
df = pd.DataFrame(
[[5, 17, 10.2],
[15, 8, 4.3],
[7, 23, 18.5],
[23, 8, 11.1]],
columns=['x', 'y', 'z'])
print('DataFrame\n-------------\n', df)
print('\nDataFrame datatypes :\n', df.dtypes)
# convert dataframe to numpy array
num_arr = df.to_numpy()
print('\nNumpy Array\n-------------\n', num_arr)
print('\nNumpy Array Datatype :', num_arr.dtype)
Output of the above code:
DataFrame
-------------
x y z
0 5 17 10.2
1 15 8 4.3
2 7 23 18.5
3 23 8 11.1
DataFrame datatypes :
x int64
y int64
z float64
dtype: object
Numpy Array
-------------
[[ 5. 17. 10.2]
[15. 8. 4.3]
[ 7. 23. 18.5]
[23. 8. 11.1]]
Numpy Array Datatype : float64
Related Articles
Python program to find the median of three valuesMerge sort program in Python
Bubble sort program in Python
Python convert list to numpy array
Python Numpy Array Shape
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Trigonometric functions Python Numpy
Python Pandas Dataframe to CSV
Python Pandas DataFrame
Convert list to dictionary Python
Python dict inside list
Convert dictionary to dataframe Python
Python Pandas CSV to Dataframe
Convert List to Dataframe Python
Python add list to list
Difference between tuple and list in Python
Convert Excel to CSV Python Pandas
Alphabetical order Python
Python | Generate QR Code using pyqrcode module