In Python, various functions and modules are used to manipulate the data efficiently, such as the Numpy module, Pandas Module, etc. Arrays store similar elements in Python, whereas the Pandas DataFrame stores different types of elements.
In Python, the multi-dimensional NumPy arrays can be converted into a 2-Dimensional Pandas Dataframe using various methods, such as pandas.DataFrame(), df.from_records(), etc.
To convert Numpy Arrays into Pandas DataFrames, the following content will be covered with examples in this write-up:
Method 1: Using pandas.DataFrame()
The “pandas.DataFrame()” Function creates a DataFrame by accepting the “NumPy Array” as an argument. Let’s understand it via the following example:
Example 1: With Default Index
In the below-given code, the “pd.DataFrame()” function is used to convert the Numpy Array to DataFrame:
Code:
import numpy
import pandas
array = numpy.array([[21,32,43],[54,65,76]])
print('Numpy Array Created: \n', array)
data_frame = pandas.DataFrame(array, columns = ['X1','X2','X3'])
print('\n DataFrame Created: \n\n',data_frame)
In the above code:
- The open source “numpy” and “pandas” packages are imported at the start.
- The “np.array()” function is used to create the “2-D” array.
- The “pandas.DataFrame()” function accepts an array as a first argument and a list of comma-separated column names as the second argument.
Output:
The above snippet verifies that the “Numpy Array” has been converted into a DataFrame.
Example 2: With Specific Index
In the following example, the “pandas.DataFrame()” function is used to convert the NumPy array to pandas DataFrame according to the specified index value.
Code:
import numpy
import pandas
array = numpy.array([[21,32,43],[54,65,76]])
print('Numpy Array Created: \n', array)
data_frame = pandas.DataFrame(array, columns = ['X1','X2','X3'], index =['Y1', 'Y2'])
print('\n DataFrame Created: \n\n',data_frame)
In the above code:
- The “index” parameter is passed as an argument to the “pd.DataFrame()” function.
- The “pandas.DataFrame()” function accepts an array, a list of comma-separated column names, and the indexes as arguments.
Output:
The above output shows that the Numpy array has been converted into Pandas DataFrame with the specified index.
Example 3: Convert Numpy Array to Pandas DataFrame With Different Data Type
We can also convert the “Numpy Array” to a different data type, such as “int” or “float”, using the “dtype” argument value.
Code:
import numpy
import pandas
array = numpy.array([[21,32,43],[54,65,76]])
print('Numpy Array Created: \n', array)
data_frame = pandas.DataFrame(array, columns = ['X1','X2','X3'], index =['Y1', 'Y2'], dtype='int')
print('\n DataFrame Created: \n\n',data_frame)
data_frame = pandas.DataFrame(array, columns = ['X1','X2','X3'], index =['Y1', 'Y2'], dtype='float')
print('\n DataFrame Created: \n\n',data_frame)
In the above code:
- In “pd.DataFrame()” function, the integer and float values are assigned to the “dtype” parameter. As a result, the stated function converts the “Numpy array” to the specified DataType format.
Output:
The above output verified that the “Numpy array” has been converted into DataFrame having integer and float values, respectively.
Method 2: Using df.from_records() Function
The “df.from_records()” function is also used to convert the structured NumPy array to pandas DataFrame:
Code:
import numpy
import pandas
array = numpy.array([['Alex',32,43],['Lily',65,76]])
print('Numpy Array Created: \n', array)
data_frame = pandas.DataFrame.from_records(array, columns = ['X1','X2','X3'], index =['Y1', 'Y2'])
print('\n DataFrame Created: \n\n',data_frame)
In the above code:
- The “pandas.DataFrame.from_records()” function accepts the array, columns, and index as arguments. Consequently, it will return a pandas DataFrame.
Output:
The above output shows that the “Numpy Array” has been converted into a DataFrame.
Conclusion
To convert the NumPy array to pandas DataFrame, the “pandas.DataFrame()” function and the “pd.DataFrame.from_records()” functions are used in Python. These functions accept the NumPy array as an argument and convert it to pandas DataFrame. This article provided an overview of converting NumPy arrays to pandas DataFrame using various examples.