Convert a Python list into a Pandas DataFrame

In this tutorial, we will learn how to convert a list into a pandas DataFrame in Python in cool and easy ways.

I know you are here because you are stuck up with a problem to find unique elements of a list then this is the best place where you can find the best ways to solve the problem.

Convert a list into a Pandas Data Frame in Python

First, let’s know what is a data form. Data from is nothing but the data that is stored in a specific format i.e in its respective rows and columns.

In order to run the program using pandas first, we have to import the pandas library.

import pandas

Next, to convert the list into the data frame we must import the Python DataFrame function.

from pandas import DataFrame

To get the data form initially we must give the data in the form of a list. For example, let us consider the list of data of names with their respective age and city

Names =  ['ASWINI', 'RITI', 'AADI']
Age   =  [24, 10, 43]
City  =  ['KOLKATA', 'DELHI', 'HYDERABAD']

Further, we have to specify the format of the data form by using the zip keyword.

This zipped data is stored in a variable so that it can be called with that variable when it is needed.

zipped_list =  list(zip(Names, Age, City))

The zipped_list is given as an input to the function DataFrame with a list of columns that represents the columns names and a list of index which represents the row names.

data_frame = pandas.DataFrame(zippedList, columns= ['Name' , 'Age', 'City'], index=['1', '2', '3'])

 

In order to view the data form  on the console, we print the data_frame

print(data_frame)

 

Below is the output of our code:

Name Age City
1 ASWINI 24 KOLKATA
2 RITI 10 DELHI
3 AADI 43 HYDERABAD

So we have successfully able to convert a Python list to a Pandas DataFrame.

Do refer the following pages for additional support in python programming:

How to find unique numbers in an array in Python

 

Leave a Reply

Your email address will not be published. Required fields are marked *