Pandas DataFrame is a 2D (two dimensional) annotated data structure in which data is aligned in the tabular form with different rows and columns. For easier understanding, the DataFrame behaves like a spreadsheet that contains three different components: index, columns, and data. Pandas DataFrames are the most common way to utilize the panda’s objects.

Pandas DataFrames can be created using different methods. This article will explain all possible methods through which you can create Pandas DataFrame in python. We have run all examples on the pycharm tool. Let’s start the implementation of each method one by one.

Basic Syntax

Follow the following syntax while creating DataFrames in Pandas python:

Example:Let’s explain with an example. In this case, we have stored the data of student’s names and percentages in a ‘Students_Data’ variable. Further, using the pd.DataFrame (), we have created a DataFrames for displaying student’s result.

import pandas as pd


Students_Data = {


   ‘Name’:[‘Samreena’, ‘Asif’, ‘Mahwish’, ‘Raees’],


   ‘Percentage’:[90,80,70,85]}


result = pd.DataFrame(Students_Data)

print (result)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/image5-11.png" data-lazy- height="163" src="data:image/svg xml,” width=”648″>

Methods to Create Pandas DataFrames

Pandas DataFrames can be created using the different ways that we will discuss in the rest of the article. We will print the Student’s courses result in the form of DataFrames. So, using one of the following methods, you can create similar DataFrames that are represented in the following image:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/image7-8.png" data-lazy- height="217" src="data:image/svg xml,” width=”552″>

Method # 01: Creating Pandas DataFrame from the dictionary of lists

In the following example, DataFrames are created from the dictionaries of lists related to student’s course results. First, import a panda’s library and then create a dictionary of lists. The dict keys represent the column names such as ‘Student_Name’, ‘Course_Title’, and ‘GPA’. Lists represent the column’s data or content. The ‘dictionary_lists’ variable contains the data of students that are further assigned to the ‘df1’ variable. Using the print statement, print the all content of DataFrames.

Example:

# Import libraries for pandas and numpy

import pandas as pd

# Import panda’s library

import pandas as pd

# Create a dictionary of list


dictionary_lists = {


   ‘Student_Name’: [‘Samreena’, ‘Raees’, ‘Sara’, ‘Sana’],


   ‘Course_Title’: [‘SQA’,‘SRE’,‘IT Basics’, ‘Artificial intelligence’],


   ‘GPA’: [3.1, 3.3, 2.8, 4.0]}

# Create the DataFrame


dframe = pd.DataFrame(dictionary_lists)

print(dframe)

After executing the above code, the following output will be displayed:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/image6-8.png" data-lazy- height="1026" src="data:image/svg xml,” width=”1916″>

Method # 02: Create Pandas DataFrame from the dictionary of NumPy array

The DataFrame can be created from the dict of array/list. For this purpose, the length must be the same as all the narray. If some index is passed, then the index length should be equal to the array’s length. If no one index is passed, then, in this case, the default index to be a range (n). Here, n represents the array’s length.

Example:

import numpy as np

# Create a numpy array


nparray = np.array(


   [[‘Samreena’, ‘Raees’, ‘Sara’, ‘Sana’],


    [‘SQA’, ‘SRE’, ‘IT Basics’,‘Artificial Intelligence’],


    [3.1, 3.3, 2.8, 4.0]])

# Create a dictionary of nparray


dictionary_of_nparray = {


   ‘Student_Name’: nparray[0],


   ‘Course_Title’: nparray[1],


   ‘GPA’: nparray[2]}

# Create the DataFrame


dframe = pd.DataFrame(dictionary_of_nparray)

print(dframe)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/image9-6.png" data-lazy- height="927" src="data:image/svg xml,” width=”1919″>

Method # 03: Creating pandas DataFrame using the list of lists

In the following code, each line represents a single row.

Example:

# Import library Pandas pd

import pandas as pd

# Create a list of lists


group_lists = [


   [‘Samreena’, ‘SQA’, 3.1],


   [‘Raees’, ‘SRE’, 3.3],


   [‘Sara’, ‘IT Basics’, 2.8],


   [‘Sana’, ‘Artificial Intelligence’, 4.0]]

# Create the DataFrame


dframe = pd.DataFrame(group_lists, columns = [‘Student_Name’, ‘Course_Title’, ‘GPA’])

print(dframe)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/image8-7.png" data-lazy- height="917" src="data:image/svg xml,” width=”1916″>

Method # 04: Creating pandas DataFrame using the list of dictionary

In the following code, each dictionary represents a single row and keys that represent the column names.

Example:

# Import library pandas

import pandas as pd

# Create a list of dictionaries


dict_list = [


   {‘Student_Name’: ‘Samreena’, ‘Course_Title’: ‘SQA’, ‘GPA’: 3.1},


   {‘Student_Name’: ‘Raees’, ‘Course_Title’: ‘SRE’, ‘GPA’: 3.3},


   {‘Student_Name’: ‘Sara’, ‘Course_Title’: ‘IT Basics’, ‘GPA’: 2.8},


   {‘Student_Name’: ‘Sana’, ‘Course_Title’: ‘Artificial Intelligence’, ‘GPA’: 4.0}]

# Create the DataFrame


dframe = pd.DataFrame(dict_list)

print(dframe)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/image2-13.png" data-lazy- height="908" src="data:image/svg xml,” width=”1864″>

Method # 05: Creating pandas Dataframe from dict of pandas Series

The dict keys represent the names of columns and each Series represents column contents. In the following lines of code, we have taken three types of series: Name_series, Course_series, and GPA_series.

Example:

# Import library pandas

import pandas as pd

# Create the Series of student names


Name_series = pd.Series([‘Samreena’, ‘Raees’, ‘Sara’, ‘Sana’])


Course_series = pd.Series([‘SQA’, ‘SRE’, ‘IT Basics’, ‘Artificial intelligence’])


GPA_series = pd.Series([3.1, 3.3, 2.8, 4.0])

# Create a Series Dictionary


dictionary_of_nparray

] = {‘Name’: Name_series, ‘Age’: Course_series, ‘Department’: GPA_series}

# DataFrame creation


dframe = pd.DataFrame(dictionary_of_nparray)

print(dframe)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/image1-13.png" data-lazy- height="923" src="data:image/svg xml,” width=”1916″>

Method # 06: Create Pandas DataFrame by using zip() function.

Different lists can be merged through the list(zip()) function. In the following example, pandas DataFrame are created by calling pd.DataFrame() function. Three different lists are created that are merged in the form of tuples.

Example:

import pandas as pd

# List1


Student_Name = [‘Samreena’, ‘Raees’, ‘Sara’, ‘Sana’]

# List2


Course_Title = [‘SQA’, ‘SRE’, ‘IT Basics’, ‘Artificial Intelligence’]

# List3


GPA = [3.1, 3.3, 2.8, 4.0]

# Take the list of tuples from three lists further, merge them by use of zip().


tuples = list(zip(Student_Name, Course_Title, GPA))

# Assign data values to tuples.


tuples

# Converting tuples list into pandas Dataframe.


dframe = pd.DataFrame(tuples, columns=[‘Student_Name’, ‘Course_Title’, ‘GPA’])

# Print data.

print(dframe)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/image4-12.png" data-lazy- height="224" src="data:image/svg xml,” width=”611″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/image3-12.png" data-lazy- height="893" src="data:image/svg xml,” width=”1919″>

Conclusion

Using the above methods, you can create Pandas DataFrames in python. We have printed a student’s course GPA by creating Pandas DataFrames. Hopefully, you will get useful results after running the above-mentioned examples. All programs are commented well for better understanding. If you have more ways to create Pandas DataFrames, then do not hesitate to share them with us. Thanks for reading this tutorial.

About the author

<img alt="Avatar" data-lazy-src="https://secure.gravatar.com/avatar/49ac9cf0c583f1d7e06205a87f549d76?s=112&r=g" height="112" src="data:image/svg xml,” width=”112″>

Samreena Aslam

Samreena Aslam holds a master’s degree in Software Engineering. Currently, she’s working as a Freelancer & Technical writer. She’s a Linux enthusiast and has written various articles on Computer programming, different Linux flavors including Ubuntu, Debian, CentOS, and Mint.