Introduction

In this tutorial, we want to create a Pandas DataFrame. In order to do this, we use the DataFrame() class of Pandas in different ways.

Import Libraries

First, we import the following python modules:

import numpy as np
import pandas as pd

Create DataFrame from Dictionary

Now, we create a Pandas DataFrame from a dictionary.

To do this, we use the DataFrame() class of Pandas to create a DataFrame object and pass a dictionary as argument. The keys of the dictionary are the column names and the values of the dictionary are the column values. The head() method of Pandas can be used to visualize the DataFrame.

data = {
    "column1": [3, 4, 1],
    "column2": [11.3, 12.5, 9.4],
    "column3": ["AI", "DL", "Python"],   
}

df = pd.DataFrame(data)
df.head()

Create DataFrame from List

Now, we create a Pandas DataFrame from a list.

To do this, we use the DataFrame() class of Pandas to create a DataFrame object and pass a list and the column names as arguments:

data = [[3, 11.3, "AI"], [4, 12.5, "DL"], [1, 9.4, "Python"]]

df = pd.DataFrame(data, columns=["column1", "column2", "column3"])
df.head()

Create DataFrame from Array

Now, we create a Pandas DataFrame from a NumPy array.

To do this, we use the DataFrame() class of Pandas to create a DataFrame object and pass a NumPy array and the column names as arguments:

data = np.array([[2, 3, 1], [9, 2, 10], [1, 1, 1]])

df = pd.DataFrame(data, columns=["column1", "column2", "column3"])
df.head()

Conclusion

Congratulations! Now you are one step closer to become an AI Expert. You have seen that it is very easy to create a Pandas DataFrame. We can simply use the DataFrame() class of Pandas. Try it yourself!

Instagram

Also check out our Instagram page. We appreciate your like or comment. Feel free to share this post with your friends.