Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
Last Updated :
21 Aug, 2020
This article demonstrates multiple examples to convert the Numpy arrays into Pandas Dataframe and to specify the index column and column headers for the data frame.
Example 1: In this example, the Pandas dataframe will be generated and proper names of index column and column headers are mentioned in the function. This approach can be used when there is no pattern in naming the index column or column headers.
Below is the implementation:
Python3
# Python program to Create a
# Pandas DataFrame from a Numpy
# array and specify the index
# column and column headers
# import required libraries
import numpy as np
import pandas as pd
# creating a numpy array
numpyArray = np.array([[15, 22, 43],
[33, 24, 56]])
# generating the Pandas dataframe
# from the Numpy array and specifying
# name of index and columns
panda_df = pd.DataFrame(data = numpyArray,
index = ["Row_1", "Row_2"],
columns = ["Column_1",
"Column_2", "Column_3"])
# printing the dataframe
print(panda_df)
Output:
Example 2: In this example, the index column and column headers are generated through iteration. The range of iterations for rows and columns are defined by the shape of the Numpy array. With every iteration, a digit will be added to the predefined string and the new index column or column header will generate. Thus, if there is some pattern in naming the labels of the dataframe this approach is suitable.
Below is the implementation:
Python3
# Python program to Create a
# Pandas DataFrame from a Numpy
# array and specify the index column
# and column headers
# import required libraries
import pandas as pd
import numpy as np
# creating a numpy array
numpyArray = np.array([[15, 22, 43],
[33, 24, 56]])
# generating the Pandas dataframe
# from the Numpy array and specifying
# name of index and columns
panda_df = pd.DataFrame(data = numpyArray[0:, 0:],
index = ['Row_' + str(i + 1)
for i in range(numpyArray.shape[0])],
columns = ['Column_' + str(i + 1)
for i in range(numpyArray.shape[1])])
# printing the dataframe
print(panda_df)
Output:
Example 3: In this example, the index column and column headers are defined before converting the Numpy array into Pandas dataframe. The label names are again generated through iterations but the method is little different. Here, the number of iterations is defined by the length of the sub-array inside the Numpy array. This method can be used if the index column and column header names follow some pattern.
Below is the implementation:
Python3
# Python program to Create a
# Pandas DataFrame from a Numpy
# array and specify the index column
# and column headers
# import required libraries
import pandas as pd
import numpy as np
# creating a numpy array
numpyArray = np.array([[15, 22, 43],
[33, 24, 56]])
# defining index for the
# Pandas dataframe
index = ['Row_' + str(i)
for i in range(1, len(numpyArray) + 1)]
# defining column headers for the
# Pandas dataframe
columns = ['Column_' + str(i)
for i in range(1, len(numpyArray[0]) + 1)]
# generating the Pandas dataframe
# from the Numpy array and specifying
# details of index and column headers
panda_df = pd.DataFrame(numpyArray ,
index = index,
columns = columns)
# printing the dataframe
print(panda_df)
Output:
Example #4: In this approach, the index column and the column headers for the Pandas dataframe will present itself in the Numpy array. During the conversion of the Numpy array into Pandas data frame, proper indexing for the sub-arrays of the Numpy array has to be done in order to get correct sequence of the dataframe labels.
Below is the implementation:
Python3
# Python program to Create a
# Pandas DataFrame from a Numpy
# array and specify the index column
# and column headers
# import required libraries
import pandas as pd
import numpy as np
# creating a numpy array and
# specifying the index and
# column headers along with
# data stored in the array
numpyArray = np.array([['', 'Column_1',
'Column_2', 'Column_3'],
['Row_1', 15, 22, 43],
['Row_2', 33, 24, 56]])
# generating the Pandas dataframe
# from the Numpy array and specifying
# details of index and column headers
panda_df = pd.DataFrame(data = numpyArray[1:, 1:],
index = numpyArray[1:, 0],
columns = numpyArray[0, 1:])
# printing the dataframe
print(panda_df)
Output:
Similar Reads
Create a DataFrame from a Numpy array and specify the index column and column headers Let us see how to create a DataFrame from a Numpy array. We will also learn how to specify the index and the column headers of the DataFrame. Approach : Import the Pandas and Numpy modules. Create a Numpy array. Create list of index values and column values for the DataFrame. Create the DataFrame. D
2 min read
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
Get column index from column name of a given Pandas DataFrame Let's learn how to get the index of a column based on its name using the DataFrame.columns attribute and Index.get_loc() method from the pandas library.Index.get_loc() Method Index.get_loc() function finds the index of a specified column name and returns an integer if the column name is unique.Synta
2 min read
Convert given Pandas series into a dataframe with its index as another column on the dataframe First of all, let we understand that what are pandas series. Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor. Syntax: pandas.Series
1 min read
How to Convert Dataframe column into an index in Python-Pandas? Pandas provide a convenient way to handle data and its transformation. Let's see how can we convert a data frame column to row name or index in Pandas. Create a dataframe first with dict of lists.  Python3 # importing pandas as pd import pandas as pd # Creating a dict of lists data = {'Name':["Akas
2 min read
How to convert index in a column of the Pandas dataframe? Each row in a dataframe (i.e level=0) has an index value i.e value from 0 to n-1 index location and there are many ways to convert these index values into a column in a pandas dataframe. First, let's create a Pandas dataframe. Here, we will create a Pandas dataframe regarding student's marks in a pa
4 min read
Creating Series from list, dictionary, and numpy array in Pandas Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). The axis labels are collectively called index. Pandas Series is nothing but a column in an excel sheet. In this article, we will see various ways of creating a series
2 min read
Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array to_numpy() method allows you to convert a Pandas DataFrame into a NumPy array, enabling efficient numerical operations, faster computations, and smooth interoperability with libraries that require NumPy arrays. Example:Pythonimport pandas as pd df = pd.DataFrame( [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1
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