Pandas dataframes are quite versatile when it comes to manipulating 2D tabular data in python. And often it can be quite useful to convert a numpy array to a pandas dataframe for manipulating or transforming data. In this tutorial, we’ll look at how to create a pandas dataframe from a numpy array.
Using the pandas.DataFrame()
function
To create a pandas dataframe from a numpy array, pass the numpy array as an argument to the pandas.DataFrame()
function. You can also pass the index and column labels for the dataframe. The following is the syntax:
df = pandas.DataFrame(data=arr, index=None, columns=None)
Examples
Let’s look at a few examples to better understand the usage of the pandas.DataFrame()
function for creating dataframes from numpy arrays.
1. 2D numpy array to a pandas dataframe
Let’s create a dataframe by passing a numpy array to the pandas.DataFrame()
function and keeping other parameters as default.
import numpy as np import pandas as pd # sample numpy array arr = np.array([[70, 90, 80], [68, 80, 93]]) # convert to pandas dataframe with default parameters df = pd.DataFrame(arr) # print print("Numpy array:\n", arr) print("\nPandas dataframe:\n", df)
Output:
Numpy array: [[70 90 80] [68 80 93]] Pandas dataframe: 0 1 2 0 70 90 80 1 68 80 93
In the above example, the dataframe df
is created from the numpy array arr
. Note that since we did not pass the index and column labels, the created dataframe used the default RangeIndex for them.
Let’s pass custom index and column labels to the dataframe being created.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
import numpy as np import pandas as pd # sample numpy array arr = np.array([[70, 90, 80], [68, 80, 93]]) # convert to pandas dataframe with custom index and column names df = pd.DataFrame(arr, columns=['History', 'Physics', 'Math'], index=['Sam', 'Emma']) # print print("Numpy array:\n", arr) print("\nPandas dataframe:\n", df)
Output:
Numpy array: [[70 90 80] [68 80 93]] Pandas dataframe: History Physics Math Sam 70 90 80 Emma 68 80 93
Here, the index labels and column names are passed to the arguments index
and columns
respectively. From the labels, we can assume that the dataframe stores the test scores of students Sam
and Emma
in the subjects History
, Physics
and Math
.
2. 1D numpy array to a pandas dataframe
Passing a one-dimensional numpy array to the pandas.DataFrame()
function will result in a pandas dataframe with one column.
import numpy as np import pandas as pd # sample numpy array arr = np.array([10, 20, 30, 40]) # convert to pandas dataframe df = pd.DataFrame(arr) # print print("Numpy array:\n", arr) print("\nPandas dataframe:\n", df)
Output:
Numpy array: [10 20 30 40] Pandas dataframe: 0 0 10 1 20 2 30 3 40
Fore more on the pandas.DataFrame()
function, refer to its official documentation.
Additional Note
Pandas dataframes are objects used to store two-dimensional tabular data. If you try to create a pandas dataframe from a numpy array with more than 2 dimensions, you’ll get an error. See the example below.
import numpy as np import pandas as pd # sample numpy array arr = np.random.randint(1,5,(3,3,2)) print("Numpy array:\n", arr) # convert to pandas dataframe df = pd.DataFrame(arr) print("\nPandas dataframe:\n", df)
Output:
Numpy array: [[[4 4] [2 2] [2 2]] [[2 2] [4 4] [1 3]] [[3 4] [4 1] [2 2]]] --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-12-8c5dce07516e> in <module> 6 print("Numpy array:\n", arr) 7 # convert to pandas dataframe ----> 8 df = pd.DataFrame(arr) 9 print("\nPandas dataframe:\n", df) ~\anaconda3\lib\site-packages\pandas\core\internals\construction.py in prep_ndarray(values, copy) 293 values = values.reshape((values.shape[0], 1)) 294 elif values.ndim != 2: --> 295 raise ValueError("Must pass 2-d input") 296 297 return values ValueError: Must pass 2-d input
* Some lines in the above error message have been skipped to shorten the output shown.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5 and pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on numpy arrays –
- How to sort a Numpy Array?
- Create Pandas DataFrame from a Numpy Array
- Different ways to Create NumPy Arrays
- Convert Numpy array to a List – With Examples
- Append Values to a Numpy Array
- Find Index of Element in Numpy Array
- Read CSV file as NumPy Array
- Filter a Numpy Array – With Examples
- Python – Randomly select value from a list
- Numpy – Sum of Values in Array
- Numpy – Elementwise sum of two arrays
- Numpy – Elementwise multiplication of two arrays
- Using the numpy linspace() method
- Using numpy vstack() to vertically stack arrays
- Numpy logspace() – Usage and Examples
- Using the numpy arange() method
- Using numpy hstack() to horizontally stack arrays
- Trim zeros from a numpy array in Python
- Get unique values and counts in a numpy array
- Horizontally split numpy array with hsplit()