

Convert NumPy Array to Pandas Dataframe – thisPointer
source link: https://thispointer.com/convert-numpy-array-to-pandas-dataframe/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

This article will discuss how to convert Numpy arrays to a Pandas DataFrame.
Table of Contents
A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method. Numpy Array is an array data structure in Python, useful for scientific computing.
Convert Numpy Array to Dataframe using pandas.DataFrame()
We can convert the Numpy Array to pandas dataframe using DataFrame() method. This is a method used to convert the dataframe available in pandas. So we have to import the pandas module.
Syntax is as follows:
Advertisements

pandas.DataFrame(array_name,columns,index)
where,
- array_name is the input numpy array which should be two dimensional array
[[elements],[elements],……….,[elements]]- The elements define the number of columns in the dataframe and the number of arrays define the number of rows.
- columns are used to specify the columns in the dataframe which are taken in the form of list separated by comma.
- [‘column_name1′,””””,’column_name n’]
- index is used to specify the rows in the dataframe which are taken in the form of list separated by comma.
- [‘row_name1′,””””,’row_name n’]
Let’s create our numpy array with 5 arrays with 2 elements each
#import numpy module import numpy #create numpy array with 5 data of students array=numpy.array([ ['sravan',7058], ['ramya',7054], ['harsha',7072], ['bobby',7053], ['kyathi',7088]]) #display print(array)
Output:
[['sravan' '7058'] ['ramya' '7054'] ['harsha' '7072'] ['bobby' '7053'] ['kyathi' '7088']]
Convert Numpy Array to pandas dataframe with default row/column labels
Here In this example we are simply converting the above array to a Pandas DataFrame.
#import pandas module import pandas #create pandas dataframe from numpy array data=pandas.DataFrame(array) #display print(data)
Output:
0 1 0 sravan 7058 1 ramya 7054 2 harsha 7072 3 bobby 7053 4 kyathi 7088
Convert Numpy Array to Pandas Dataframe with Column and Row Names
Here In this example we are simply converting the above array to Pandas DataFrame and specift rows and columns
#import pandas module import pandas # create pandas dataframe from numpy array by specifying rows and columns # row name starts from row1 to row5 # Column names are 'Name'and 'Roll no' data=pandas.DataFrame( array, columns=['Name','Roll no'], index=['row1','row2','row3','row4','row5']) #display print(data)
Output:
Name Roll no row1 sravan 7058 row2 ramya 7054 row3 harsha 7072 row4 bobby 7053 row5 kyathi 7088
Here we are specifying column names as Name and Roll no and rows as row1 to row5.
Convert 2D Numpy Array to Pandas DataFrame
Here we are going to consider an two dimensional numpy array and convert into a Dataframe. A 2D Numpy array has n rows and n columns . we can convert to dataframe by using these rows and columns. So these will form a row and column in pandas dataframe.
First we will create an two dimensional numpy array for a range of integers using arange() function with 2 rows and 5 columns.
#import numpy module import numpy #create 10 elements with 2 rows and 5 columns array= numpy.arange(10).reshape(2,5) #display print(array)
Output:
[[0 1 2 3 4] [5 6 7 8 9]]
Now, we will convert into pandas dataframe.
#import pandas import pandas as pd #convert the numpy array to pandas dataframe data=pd.DataFrame( array, columns=['col1','col2','col3','col4','col5'], index=['row1','row2']) #display print(data)
Output:
col1 col2 col3 col4 col5 row1 0 1 2 3 4 row2 5 6 7 8 9
Here we specified the row names as row 1 to row n and column names as col1 to col n.
Convert 2D Numpy Array tp Dataframe with different types
Here we will create a two-dimensional numpy array with different data types and convert it into a dataframe. The 2D Numpy array has n rows and n columns. We can convert it to a dataframe. These rows and columns of the 2D Numpy Array will be the rows and columns of the pandas Dataframe.
Let’s create a two-dimensional numpy array using a set of integers with one array as int type and another as column type and convert it into dataframe
#import numpy module import numpy #create 10 elements with 2 rows and 5 columns array= numpy.array([[23, 45, 43, 23, 21], [45.6, 32.5, 45.6, 6.7, 8.9]]) #display print(array)
Output:
[[23. 45. 43. 23. 21. ] [45.6 32.5 45.6 6.7 8.9]]
Now, we will convert this into pandas dataframes of float and integer types and integer type. We can do this by using the dtype parameter.
- To convert to float – use dtype=’float’
- To convert to integer – use dtype=’int’
Let’s see the code
#import pandas import pandas as pd #convert the numpy array to pandas dataframe with integer type data=pd.DataFrame( array, columns=['col1','col2','col3','col4','col5'], index=['row1','row2'], dtype='int') #display print(data) #convert the numpy array to pandas dataframe with float type data=pd.DataFrame( array, columns=['col1','col2','col3','col4','col5'], index=['row1','row2'], dtype='float') #display print(data)
Output:
col1 col2 col3 col4 col5 row1 23 45 43 23 21 row2 45 32 45 6 8 col1 col2 col3 col4 col5 row1 23.0 45.0 43.0 23.0 21.0 row2 45.6 32.5 45.6 6.7 8.9
Summary
This article discussed five approaches for converting numpy array to pandas DataFrame using pandas.DataFrame() with examples.
Advertisements
Recommend
-
3
Pandas Dataframe.loc[] – thisPointer Skip to content In this article, we will discuss how to use the loc property of the Dataframe with examples. In Pandas, t...
-
4
Pandas Dataframe.iloc[] – thisPointer Skip to content In this article, we will discuss how to use the iloc property of Dataframe with examples. In Pandas, th...
-
12
This article will discuss how to convert Pandas Dataframe to Numpy Array. Table of Contents A Dataframe is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.Data...
-
5
Pretty Print a Pandas Dataframe – thisPointer Skip to content In this article we will discuss how to print the a Dataframe in pretty formats.
-
9
This article will discuss how to convert JSON to pandas Dataframe. JSON stands for JavaScript Object Notation that stores the data in key-value pair format, inside the list/dictionary data structure. A DataFrame is a data structure tha...
-
9
Export Pandas Dataframe to JSON – thisPointer Skip to content In this article, we will discuss how to export a Pandas Dataframe to JSON file in Python.
-
9
Drop Duplicate Rows from Pandas Dataframe – thisPointer In this article, we will discuss different ways to delete duplicate rows in a pandas DataFrame. Table of Contents: A DataFrame is a data structure...
-
12
Convert the Column type from String to Datetime format in Pandas DataframeConvert the Column type from String to Datetime format in Pandas Dataframe10 Views30/05/2022...
-
7
In this article, we will discuss different ways to convert a Pandas Series or Index to a NumPy array in Python. Table Of Contents Overview of a Pandas Series In Pandas, a Se...
-
3
pandas.DataFrame 转成 numpy.array 以及 C++ 的二进制文件 作者:
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK