In this post, we are going to understand how to convert the 2D NumPy array to Pandas dataframe. The panda’s library pandas.dataframe() method used to convert numpy array to pandas dataframe.Let’s first understand the dataframe() method to perform any operation.
Pandas.dataframe() function
The pandas. dataframe() method data,index,columns,type to convert list,array,dict and pandas series to pandas dataframe.
pandas.DataFrame(data=None, index=None, columns=None, dtype=None)
Parameters
- data: It can be a list, array,dict, and pandas series
- Index: It is used to index the dataframe default values is rangeindex(0,1,2…..n.)
- columns:It is used to label dataframe column.The defaulting to RangeIndex(0, 1, 2, …, n).
- datatye:Used to specify the datatype,default is None
1. 2D NumPy array to Pandas DataFrame
In this example, we are converting numpy array to dataframe without column name and row name to dataframe using pandas.dataframe() function and passing numpy array as agrument.
import numpy as np
import pandas as pd
myarr = np.array([ ['Tom', 9, 8, 'WA'], ['Trex', 6, 15, 'CA'], ['Kity', 7, 11, 'WA']])
dfobj = pd.DataFrame(myarr)
print(dfobj)
Output
0 1 2 3
0 Tom 9 8 WA
1 Trex 6 15 CA
2 Kity 7 11 WA
<class 'pandas.core.frame.DataFrame'>
2. 2D NumPy array to pandas dataframe with column name
In this python program, we will understand how to convert 2D numpy array to pandas dataframe with column name/header
- To create dataframe with columnname/header we have to pass a list of columns name to pandas dataframe parameters ‘columns’.The default value for column is rangeindex(0,1,2…….n).in below code we are passing custom columname
import numpy as np
import pandas as pd
myarr = np.array([ ['Tom', 9, 8, 'WA'], ['Trex', 6, 15, 'CA'], ['Kity', 7, 11, 'WA']])
dfobj = pd.DataFrame(myarr, columns = ['Name', 'Age', 'Weight', 'City'])
print(dfobj)
Output
Name Age Weight City
0 Tom 9 8 WA
1 Trex 6 15 CA
2 Kity 7 11 WA
3. Numpy array to dataframe with index
Sometimes we have to convert numpy array to pandas dataframe with index. The pandas. dataframe() method index parameter is used to pass the custom indexes label to the pandas dataframe. The default value for index is rangindex(0,1,2….)
import numpy as np
import pandas as pd
myarr = np.array([ ['Tom', 9, 8, 'WA'], ['Trex', 6, 15, 'CA'], ['Kity', 7, 11, 'WA']])
dfobj = pd.DataFrame(myarr, columns = ['Name', 'Age', 'Weight', 'City'],index=['Row_1','Row_2','Row_3'])
print(dfobj)
Output
Name Age Weight City
Row_1 Tom 9 8 WA
Row_2 Trex 6 15 CA
Row_3 Kity 7 11 WA
4. NumPy to dataframe without index
In this example, we are converting numpy array to pandas dataframe without index, First, we have created pandas dataframe by using pd.dataframe() method and later set index off by using to_string(index=False).
import numpy as np
import pandas as pd
myarr = np.array([ ['Tom', 9, 8, 'WA'], ['Trex', 6, 15, 'CA'], ['Kity', 7, 11, 'WA']])
dfobj = pd.DataFrame(myarr, columns = ['Name', 'Age', 'Weight', 'City'])
print(dfobj.to_string(index=False))
Output
Name Age Weight City
Tom 9 8 WA
Trex 6 15 CA
Kity 7 11 WA
5. 2D NumPy array to Pandas DataFrame with differnt type
Sometime we have to convert numpy array to dataframe with column name and row index by passing columns a list of columnname to columns parameter of dataframe() function, row index as a list of rowindex to index parameter of dataframe() function.
- We have set different type of each column by using dfobj[‘Name’].astype(str) for string datatype.
- We have set different type of each column by using dfobj[‘Name’].astype(int) for int datatype.
import numpy as np
import pandas as pd
myarr = np.array([ ['Tom', 9, 8, 'WA'], ['Trex', 6, 15, 'CA'], ['Kity', 7, 11, 'WA']])
dfobj = pd.DataFrame(myarr, columns = ['Name', 'Age', 'Weight', 'City'],index=['Row_1','Row_2','Row_3'])
dfobj['Name'] = dfobj['Name'].astype(str)
dfobj['Age'] = dfobj['Age'].astype(int)
dfobj['Weight'] = dfobj['Weight'].astype(int)
dfobj['City'] = dfobj['City'].astype(str)
print(dfobj)
print('\n',dfobj.dtypes)
Output
Name Age Weight City
Row_1 Tom 9 8 WA
Row_2 Trex 6 15 CA
Row_3 Kity 7 11 WA
Name object
Age int32
Weight int32
City object
dtype: object
Summary
In this post, we have learned how to convert 2D NumPy array to Pandas DataFrame by using 5 different examples.