Create a Pandas DataFrame from a Numpy array and specify the index column and column headers
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
import numpy as np
import pandas as pd
numpyArray = np.array([[ 15 , 22 , 43 ],
[ 33 , 24 , 56 ]])
panda_df = pd.DataFrame(data = numpyArray,
index = [ "Row_1" , "Row_2" ],
columns = [ "Column_1" ,
"Column_2" , "Column_3" ])
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
import pandas as pd
import numpy as np
numpyArray = np.array([[ 15 , 22 , 43 ],
[ 33 , 24 , 56 ]])
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 ])])
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
import pandas as pd
import numpy as np
numpyArray = np.array([[ 15 , 22 , 43 ],
[ 33 , 24 , 56 ]])
index = [ 'Row_' + str (i)
for i in range ( 1 , len (numpyArray) + 1 )]
columns = [ 'Column_' + str (i)
for i in range ( 1 , len (numpyArray[ 0 ]) + 1 )]
panda_df = pd.DataFrame(numpyArray ,
index = index,
columns = columns)
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
import pandas as pd
import numpy as np
numpyArray = np.array([[' ', ' Column_1',
'Column_2' , 'Column_3' ],
[ 'Row_1' , 15 , 22 , 43 ],
[ 'Row_2' , 33 , 24 , 56 ]])
panda_df = pd.DataFrame(data = numpyArray[ 1 :, 1 :],
index = numpyArray[ 1 :, 0 ],
columns = numpyArray[ 0 , 1 :])
print (panda_df)
|
Output:
Last Updated :
21 Aug, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...