Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array Last Updated : 21 Apr, 2025 Comments Improve Suggest changes Like Article Like Report to_numpy() method converts Pandas DataFrame into a NumPy array. This method is used when we need to perform numerical operations that require a NumPy array instead of a DataFrame structure. In this article, we will see how to convert a dataframe to anumpy array.Syntax: Dataframe.to_numpy(dtype = None, copy = False) Parameters: dtype: Data type which we are passing like str. copy: [bool, default False] ensures that a new array is created otherwise, it might return a view of the original data.Returns- numpy.ndarray: NumPy array representation of the DataFrame.Example 1: Convert Full DataFrame to NumPy Arraydf.to_numpy(): Converts a Pandas DataFrame into a NumPy array containing same data without index or column labels. Python import pandas as pd df = pd.DataFrame( [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], columns=['a', 'b', 'c']) arr = df.to_numpy() print('\nNumpy Array\n----------\n', arr) print(type(arr)) Output: Full DataFrame to NumPy ArrayExample 2: Convert Specific Columns to NumPy Arrayarr = df[['a', 'c']].to_numpy(): Converts selected columns 'a' and 'c' from DataFrame df into a NumPy array which is only containing data from those two columns. Python import pandas as pd df = pd.DataFrame( [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], columns=['a', 'b', 'c']) arr = df[['a', 'c']].to_numpy() print('\nNumpy Array\n----------\n', arr) print(type(arr)) Output:Specific Columns to NumPy ArrayExample 3: Convert DataFrame with Mixed Data Typesarr = df.to_numpy(): Converts entire DataFrame df into a NumPy array which is containing all the data from DataFrame without index and column labels. Python import pandas as pd import numpy as np df = pd.DataFrame( [[1, 2, 3], [4, 5, 6.5], [7, 8.5, 9], [10, 11, 12]], columns=['a', 'b', 'c']) arr = df.to_numpy() print('Numpy Array', arr) print('Numpy Array Datatype :', arr.dtype) Output:DataFrame with Mixed Data TypesExample 4: Convert DataFrame Using CSV FileWe are using a CSV file for changing Dataframe into a Numpy array.You can download the dataset from here-nba.csv.data.dropna(inplace=True): Removes any rows with missing values (NaN) from the DataFrame data and updates DataFrame in place.df = pd.DataFrame(data['Weight'].head()): Creates a new DataFrame df containing only first 5 values from the 'Weight' column of data. The head() function selects first 5 rows by default. Python import pandas as pd data = pd.read_csv("/content/nba.csv") data.dropna(inplace=True) df = pd.DataFrame(data['Weight'].head()) print(df.to_numpy()) Output:DataFrame Using CSV FileExample 5: Convert with Specified Data Type In this example, we are just providing the parameters in the same code to provide dtype here.df.to_numpy(dtype='float32'): Converts DataFrame df into a NumPy array with data type explicitly set to float32 which ensures values in the resulting array are stored as 32-bit floating-point numbers. Python import pandas as pd data = pd.read_csv("/content/nba.csv") data.dropna(inplace=True) df = pd.DataFrame(data['Weight'].head()) print(df.to_numpy(dtype='float32')) Output: Specified Data TypeExample 6: Validate Type of Array After Conversiontype(df.to_numpy()): Returns type of the object resulting from the to_numpy() method which is a <class 'numpy.ndarray'> indicating that DataFrame df has been converted into a NumPy array. Python import pandas as pd data = pd.read_csv("/content/nba.csv") data.dropna(inplace=True) df = pd.DataFrame(data['Weight'].head()) print(type(df.to_numpy())) Output:<class 'numpy.ndarray'>With df.to_numpy() we can easily transform a Pandas DataFrame into a NumPy array for efficient data manipulation and numerical operations. This method ensures integration with other libraries that are dependent on NumPy arrays for computations. Comment More infoAdvertise with us Next Article Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array N nikhilaggarwal3 Follow Improve Article Tags : Python Python-pandas Python pandas-dataFrame Practice Tags : python Similar Reads Convert a NumPy array to Pandas dataframe with headers To convert a numpy array to pandas dataframe, we use pandas.DataFrame() function of Python Pandas library. Syntax: pandas.DataFrame(data=None, index=None, columns=None) Parameters: data: numpy ndarray, dict or dataframe index: index for resulting dataframe columns: column labels for resulting datafr 1 min read Pandas DataFrame to_dict() Method | Convert DataFrame to Dictionary Python is a great language for doing data analysis because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas .to_dict() method is used to convert a DataFrame into a dictionary of series or list-like 3 min read How to convert Dictionary to Pandas Dataframe? Converting a dictionary into a Pandas DataFrame is simple and effective. You can easily convert a dictionary with key-value pairs into a tabular format for easy data analysis. Lets see how we can do it using various methods in Pandas.1. Using the Pandas ConstructorWe can convert a dictionary into Da 2 min read NumPy ndarray.tolist() Method | Convert NumPy Array to List The ndarray.tolist() method converts a NumPy array into a nested Python list. It returns the array as an a.ndim-levels deep nested list of Python scalars. Data items are converted to the nearest compatible built-in Python type. Example Python3 import numpy as np gfg = np.array([1, 2, 3, 4, 5]) print 1 min read How to Convert Float to Datetime in Pandas DataFrame? Pandas Dataframe provides the freedom to change the data type of column values. We can change them from Integers to Float type, Integer to Datetime, String to Integer, Float to Datetime, etc. For converting float to DateTime we use pandas.to_datetime() function and following syntax is used : Syntax: 3 min read How to Convert Integer to Datetime in Pandas DataFrame? Let's discuss how to convert an Integer to Datetime in it. Now to convert Integers to Datetime in Pandas DataFrame. Syntax of  pd.to_datetimedf['DataFrame Column'] = pd.to_datetime(df['DataFrame Column'], format=specify your format)Create the DataFrame to Convert Integer to Datetime in Pandas Check 2 min read Convert Floats to Integers in a Pandas DataFrame Let us see how to convert float to integer in a Pandas DataFrame. We will be using the astype() method to do this. It can also be done using the apply() method. Convert Floats to Integers in a Pandas DataFrameBelow are the ways by which we can convert floats to integers in a Pandas DataFrame: Using 3 min read How to convert NumPy array to dictionary in Python? The following article explains how to convert numpy array to dictionary in Python. Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array. A tuple of integers givi 3 min read How to Convert Index to Column in Pandas Dataframe? Pandas is a powerful tool which is used for data analysis and is built on top of the python library. The Pandas library enables users to create and manipulate dataframes (Tables of data) and time series effectively and efficiently. These dataframes can be used for training and testing machine learni 2 min read How to Convert String to Float in Pandas DataFrame Converting Strings to Float in Pandas DataFrame is a very crucial step for data analysis. Converting string to float values can help you perform various arithmetic operations and plot graphs. In this article, we'll look at different ways to convert a string to a float in DataFrame. Creating Sample D 4 min read Like