• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / How to Convert a NumPy Array to Pandas Dataframe

How to Convert a NumPy Array to Pandas Dataframe

April 10, 2021 by cmdlinetips

Convert Numpy Arrays to Pandas Dataframe
Convert Numpy Arrays to Pandas Dataframe
NumPy and Pandas are two most useful python toolkits for data analysis. Sometimes you might want to convert a 2d-array in numpy to a dataframe.

In this short tutorial, we will learn how to convert a numpy array into Pandas dataframe.

import pandas as pd
import seaborn as sns

Let us first create some numpy array. We will use NumPy’s random module to create two-dimesional numpy array.

np_array = np.random.rand(10,3)

Here we have created two dimesional numpy array of shape 10 rows x 3 columns

np_array.shape
(10, 3)

To convert a numpy array to a Pandas dataframe, we use Pandas’ DataFrame() function with the numpy array as argument.

# convert numpy array to Pandas dataframe
pd.DataFrame(np_array)

We get a Pandas dataframe with default column names and index or row names. By default, Pandas DataFrame() function names the columns starting with index 0.

             0	              1 	2
0	0.240193	0.390997	0.233373
1	0.562184	0.964387	0.146074
2	0.542980	0.498600	0.494699
3	0.764410	0.429342	0.450513
4	0.595966	0.805123	0.114175
5	0.062249	0.334657	0.185373
6	0.904895	0.534821	0.087906
7	0.425533	0.472328	0.929547
8	0.209767	0.853591	0.522343
9	0.234314	0.732298	0.010851

If you wanted specific column names while creating the dataframe, we can provide the column names as “column” argument to DataFrame() function.

# convert numpy array to Pandas dataframe with column names
pd.DataFrame(np_array, columns=["c1","c2","c3"])

In this example, we provided a list of names for columns.

	      c1	      c2	c3
0	0.240193	0.390997	0.233373
1	0.562184	0.964387	0.146074
2	0.542980	0.498600	0.494699
3	0.764410	0.429342	0.450513
4	0.595966	0.805123	0.114175
5	0.062249	0.334657	0.185373
6	0.904895	0.534821	0.087906
7	0.425533	0.472328	0.929547
8	0.209767	0.853591	0.522343
9	0.234314	0.732298	0.010851

You might also want to check out how to rename Pandas’ colnames using dictionary here. How to Rename Columns in Pandas?

Want to get better at using Pandas for data science-ing? Check out Byte Sized Pandas 101 tutorials.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to Convert Wide Dataframe to Tidy Dataframe with Pandas stack()? Pandas Convert Two Columns to a DictionaryHow To Convert Pandas Dataframe to a Dictionary Default ThumbnailHow to Convert a Column to Datetime type with Pandas Default Thumbnail12 Basic Commands with NumPy Array

Filed Under: Pandas 101, Python, Python Tips Tagged With: Numpy Array to Pandas, Pandas 101, Python

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version