Python – How to Create Dataframe using Numpy Array

0

In this post, you will get a code sample for creating a Pandas Dataframe using a Numpy array with Python programming.

Step 1: Load the Python Packages

import numpy as np
import pandas as pd

Step 2: Create a Numpy array

arr = np.array([[4, 7], [15,18], 
                [18,21], [13,19], 
                [10,15], [7,12], 
                [4,6], [5,9], [8,10], [9,14], [13,15], [11,12], [12,17]])

This is how the array would look like:

array([[ 4,  7],
       [15, 18],
       [18, 21],
       [13, 19],
       [10, 15],
       [ 7, 12],
       [ 4,  6],
       [ 5,  9],
       [ 8, 10],
       [ 9, 14],
       [13, 15],
       [11, 12],
       [12, 17]])

Step 3: Create a Transpose of Numpy Array

arr_tp = arr.transpose()

This is how the transpose would look like:

array([[ 4, 15, 18, 13, 10,  7,  4,  5,  8,  9, 13, 11, 12],
       [ 7, 18, 21, 19, 15, 12,  6,  9, 10, 14, 15, 12, 17]])

Step 4: Create a Pandas Dataframe

df = pd.DataFrame({'col1': arr_tp[0], 'col2': arr_tp[1]})

Print the data using head command such as df.head(). This is how the data frame would look like:

      col1	col2
0	4	7
1	15	18
2	18	21
3	13	19
4	10	15

In case, you would like to quickly plot the data and look for relationship, here are the command using seaborn package:

import seaborn as sns
sns.scatterplot(x=df['col1'], y=df['col2'])

The above would print the following plot:

Seaborn Scatterplot
Fig 1: Scatterplot representing the relationship between col1 and col2
Ajitesh Kumar
Share.

Leave A Reply

Time limit is exhausted. Please reload the CAPTCHA.