Create Pandas DataFrame Using NumPy Array – Python Pandas Tutorial

By | December 8, 2022

Sometimes, we have to create a pandas dataframe using a numpy array. In this tutorial, we will introduce you how to do.

How to create a pandas dataframe using numpy array?

A pandas dataframe may looks like:

Create Pandas DataFrame Using NumPy Array - Python Pandas Tutorial

To create a dataframe using numpy array, we should notice:

  • we need a column name for each column
  • each column saves a column of numpy array

We will use an example to show you how to do.

For example:

import pandas as pd
import numpy as np

data = np.random.random((5,4))

df = pd.DataFrame(data, columns = ["head1", "head2", "head3", "head4"])
print(df)

Here the shape of data is [5, 4], which means 5 rows and 4 columns.

It determines the column number of df is 4.

Meanwhile, we also can append a column for df as follows:

dx = np.array(["test1","test2","test3","test4","test5",])

df["head5"] = dx

print(df)

Run this code, we will see:

      head1     head2     head3     head4  head5
0  0.555170  0.139445  0.201438  0.922460  test1
1  0.525524  0.806295  0.264646  0.476339  test2
2  0.664801  0.844180  0.201969  0.399945  test3
3  0.764095  0.492824  0.916792  0.740180  test4
4  0.767567  0.222740  0.364453  0.062312  test5