Notice: Constant automattic\jetpack\extensions\social_previews\FEATURE_NAME already defined in /home/beaczwhx/dggulaitutorial.com/wp-content/plugins/jetpack/extensions/blocks/social-previews/social-previews.php on line 14
How To Create Python Pandas Dataframe From Numpy Array - Dggul AI Tutorial

How To Create Python Pandas Dataframe From Numpy Array


Notice: Undefined index: limited_lang in /home/beaczwhx/dggulaitutorial.com/wp-content/plugins/code-syntax-highlighter/inc/src/rendrer.php on line 297

Hello Coders, in previous tutorials we have learned creating pandas dataframe from list and CSV file. In this tutorial we will learn How To Create Python Pandas Dataframe From NumPy Array. 

If you want to learn creating pandas dataframe from list and csv file then check below tutorials.

How To Create Pandas Dataframe From List In Python

How To Create Dataframe From CSV File In Python

Now let’s jump to How do you create a pandas DataFrame from a NumPy array in Python Tutorial. But before proceeding to the coding part we will know little bit about NumPy . So let’s get started.

What Is NumPy?

NumPy is a very popular library of python which is used in scientific computing and many more.

  • NumPy is used to create multidimensional array in python.
  • In 2005, Travis Oliphant had created NumPy package.
  • NumPy stands for “Numeric Python” or “Numerical Python”
  • NumPy array are stored at one continuous place in memory that’s why it is faster than list.

Why To Use NumPy?

  • Python doesn’t support multidimensional arrays directly so we have to use a third party module that is NumPy to work with multidimensional arrays in python.
  • NumPy uses much less memory to store data and it provides a mechanism of specifying the data types.
  • It provides ndarray, a homogeneous n-dimensional array object, with methods to efficiently operate on it

How To Create Python Pandas Dataframe From NumPy Array In Different ways

We can create pandas dataframe from NumPy array in different ways such as with column names,Index etc. Let’s learn all of them in detail.

But to convert NumPy ndarray to dataframe python, firstly we will have to create NumPy array.

Creating A NumPy Array

Firstly we will create a simple NumPy array and then use it for creating pandas dataframe. So write the following code on Jupyter Notebook or on any IDEs on which you work.

import numpy as np

np_array = np.array([[20,40,50],[60,80,90]])

print(np_array)

What We Did?

  • First of all we have imported NumPy package.
  • Then created NumPy array using array() method. We have passed a nested list to create 2D array.
  • And finally printed the array.

Output

Python Pandas Dataframe From Numpy Array

 

We have created NumPy array successfully now Let’s create pandas dataframe from this NumPy array.

How to Create Pandas Dataframe From NumPy Array

import pandas as pd
import numpy as np

# Creating NumPy array
np_array = np.array([[20,40,50],[60,80,90]])


# Creating Dataframe From NumPy Array
df_from_numpyarray = pd.DataFrame(np_array)

print("Dataframe From NumPy Array\n")

# Printing Dataframe
print(df_from_numpyarray)

What We Did?

  • First of all imported pandas module.
  • Then created a NumPy array which we have discussed previously.
  • Then created dataframe from this NumPy array using DataFrame() method.
  • And finally printed the dataframe.

Output

Let’s see it’s output.

Python Pandas Dataframe From Numpy Array

 

Create Pandas Dataframe From NumPy Array With Column Names

You might have noticed in above output, the column names are 1,2,3 which are by default. Now what if we want to specify our own column names.

To do that we have to pass columns attribute in DataFrame() method. This will create dataframe with custom column names.

So run the following code snippets to Convert NumPy array to pandas dataframe with column name.

import pandas as pd
import numpy as np
np_array = np.array([[20,40,50],[60,80,90]])

df_from_numpyarray = pd.DataFrame(np_array, columns = ['A', 'B', 'C'])

print("Dataframe From NumPy Array With Column Name\n")

print(df_from_numpyarray)

Let’s check it’s output.

Python Pandas Dataframe From Numpy Array

 

But one thing you should remember white creating dataframe from NumPy array with column names is that you should enter column name as long as the number of columns in the array.

Let’s understand it with the example of NumPy array which we have created above. In this array, we have inserted three values that means this array have three columns. So we have inserted three column names A, B and C to create dataframe from this NumPy array.

Create Pandas Dataframe From NumPy Array With Index

Now if we have to create dataframe from NumPy array with index value then what will we do for it. Let’s run the following code.

import pandas as pd
import numpy as np
np_array = np.array([[20,40,50],[60,80,90]])

df_from_numpyarray = pd.DataFrame(np_array, columns = ['A', 'B', 'C'], index = ['1','2'])

print("Dataframe From NumPy Array With Column Name And Index\n")

print(df_from_numpyarray)

What We Did?

  • We have passed index argument into the DataFrame() method.

Now let’s see its output. So it’s output is as follows.

 

So you can see our own custom index has been assigned to each row.

Create Pandas Dataframe From NumPy Array of Mix of Strings and Numeric Data

So guys till now we have learned to create dataframe from NumPy array of same data type. But what if we have a NumPy array of mixed datatypes.

To create pandas dataframe from NumPy array of mixed data types, we will write following code.

import pandas as pd
import numpy as np

np_array = np.array([['John', 45, 2020, 50000],['Venus', 25, 2021, 40000]], dtype = object)

df_from_numpyarray = pd.DataFrame(np_array, columns = ['Name', 'Age', 'DOJ','Salary'], index = ['1','2'])

print("Dataframe From NumPy Array of mixed datatypes\n")
print(df_from_numpyarray)
  • As we are creating NumPy array with mixed data that’s why we have passed the argument dtype=object.
  • Now our NumPy array dtype is object so we can create dataframe from this array.

Let’s see its output.

 

So guys this was all about Python Pandas Dataframe From NumPy Array Tutorial. I hope this tutorial have helped you . If so then share this post with your friends who are learning pandas library.  And if you have any query regarding this post then feel free to ask in comments.

Leave a Comment