Numpy arrays in Python are used for storing and manipulating data. In Python, different operations such as adding, removing, appending, and others are performed on Numpy using various inbuilt functions. However, sometimes while working with the Numpy array, it is required to convert it into Pandas DataFrame for various data analysis operations. Python provides several methods to convert an array to DataFrame.
This write-up will give you a detailed guide on converting an array to Pandas DataFrame using multiple examples.
How to Convert Array to Python Pandas DataFrame?
To create the Pandas DataFrame by accepting the specified array, the below methods are used in Python:
Method 1: Convert Array to Pandas DataFrame Using “pd.DataFrame()” Function
The “pd.DataFrame()” function of the “Pandas” module is used to create the DataFrame in Python. This method can accept the array as an argument and convert/transform it into Pandas DataFrame. For further understanding, look at the below example code:
Example 1: Convert Array to Pandas DataFrame
The following example is utilized to create the DataFrame by taking the array:
arr1 = numpy.array([[32,42,12],[32,12,18],[82,52,58]])
print(arr1, '\n')
print(pandas.DataFrame(arr1, columns = ['Team_1','Team_2','Team_3']))
In the above code:
- The “numpy” and “pandas” module is imported.
- The “numpy.array()” method takes the data as an argument and creates the numpy array.
- The “pandas.DataFrame()” function accepts the “array” and “columns” names as an argument to create the DataFrame with a specified column.
Output
The specified array has been transformed/converted into DataFrame.
Example 2: Convert Array to Pandas DataFrame by Adding Custom Index Value
Let’s overview the below code:
arr1 = numpy.array([[32,42,12],[32,12,18],[82,52,58]])
print(arr1, '\n')
print(pandas.DataFrame(arr1, columns = ['Team_1','Team_2','Team_3'], index=['A', 'B', 'C']))
In the above code:
- The “pandas.DataFrame()” method takes the array, column, and index parameters as an argument to create the DataFrame with a custom index.
Output
The DataFrame with a custom index has been created.
Example 3: Convert Mixed Array to DataFrame of Pandas
The following code converts the Numpy array containing mixed data type value to Pandas DataFrame:
arr1 = numpy.array([['Joseph',21,5.2],['Anna',22,4.8],['Lily',19,5.8]])
print(arr1, '\n')
print(pandas.DataFrame(arr1, columns = ['Name','Age','Height']))
In this code:
- The “numpy.array()” method creates a mixed array and assigns the value to the “arr1” variable.
- The “pandas.DataFrame()” method takes the array and column value as an argument and gets the Pandas DataFrame.
Output
Method 2: Convert Array to Pandas DataFrame Using “from_records()” Function
The “from_records()” function can also be utilized to convert/transform the array to Pandas DataFrame. Here is an example:
arr1 = numpy.array([[32,42,12],[32,12,18],[82,52,58]])
print(arr1, '\n')
print(pandas.DataFrame.from_records(arr1, columns = ['Team_1','Team_2','Team_3']))
In this code:
- The “pandas.DataFrame.from_records()” function takes the “array” as an argument and retrieves/gets the DataFrame.
Output
The data frame has been successfully created from the given array.
Conclusion
In Python, the “pandas.DataFrame()” and “from_records()” methods are utilized to convert/transform the array to Pandas DataFrame. The “pd.DataFrame()” method can also be used to add columns and custom indexes while converting the array to DataFrame. This write-up delivered a comprehensive guide on converting a numpy array to Pandas DataFrame using numerous examples.