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.
Students_Data = {
'Name':['Samreena', 'Asif', 'Mahwish', 'Raees'],
'Percentage':[90,80,70,85]}
result = pd.DataFrame(Students_Data)
print (result)
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:
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 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:
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:
# 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)
Method # 03: Creating pandas DataFrame using the list of lists
In the following code, each line represents a single row.
Example:
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)
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 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)
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 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)
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:
# 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)
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.