How to Convert NumPy Array to pandas DataFrame
In this tutorial, you will convert a NumPy array to a pandas DataFrame
TLDR solution
import numpy as np
import pandas as pd
array = np.array([[a1, b1, c1], [a2, b2, c3]])
df = pd.DataFrame(my_array, columns=["A", "B", "C"])
Install NumPy and pandas
If you don't have NumPy and pandas already installed, execute the following command in your terminal:
pip install numpy pandas
Example 1: Convert a Numerical NumPy Array
Let's say, you have a NumPy array consisting of only numbers, e.g., count of caught salmons, pufferfish and sharks, respectively:
import numpy as np
array = np.array([[500, 10, 1], [1000, 12, 0]])
You can then convert it to a DataFrame like this:
import pandas as pd
columns = ["salmon", "pufferfish", "shark"]
df = pd.DataFrame(array, columns=columns)
print(df)
print(type(df))
The output of this code should look like this:
salmon pufferfish shark
0 500 10 1
1 1000 12 0
<class 'pandas.core.frame.DataFrame'>
If you want to add a custom index, add the index option:
df = pd.DataFrame(array, columns=columns, index=["boat1", "boat2"])
To get:
salmon pufferfish shark
boat1 500 10 1
boat2 1000 12 0
Example 2: Convert a Mixed-Type NumPy Array
Let's say, you have a NumPy array consisting of strings and numbers, e.g., boat name, count of caught salmons, pufferfish and sharks, respectively:
import numpy as np
array = np.array([['boat1', 500, 10, 1], ['boat2', 1000, 12, 0]])
You can then convert it to a DataFrame like this:
import pandas as pd
columns = ["boat", "salmon", "pufferfish", "shark"]
df = pd.DataFrame(array, columns=columns)
print(df)
print(df.dtypes) # check data type of columns
To get:
boat salmon pufferfish shark
0 boat1 500 10 1
1 boat2 1000 12 0
boat object
salmon object
pufferfish object
shark object
dtype: object
Notice that, all columns are of object/string type. Convert the numerical columns like this:
df['salmon'] = df['salmon'].astype('int')
df['pufferfish'] = df['pufferfish'].astype('int')
df['shark'] = df['shark'].astype('int')
print(df.dtypes)
To get:
boat object
salmon int64
pufferfish int64
shark int64
dtype: object
That's it! You just learned how to convert a NumPy array to a pandas DataFrame.