Articles

How to convert lists to pandas DataFrame

How to convert lists to pandas DataFrame


In this pandas tutorial, we will discuss how to create pandas dataframe from list data structure.

 

Introduction

List in python is an one dimensional data structure that can hold multiple data type  elements. We can create a list by using - [].

DataFrame in pandas is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe.

Here, the data can be the list.

Scenario -1 : Convert list of lists to pandas DataFrame

Here, we are converting collection of lists to pandas dataframe.

These collection is placed inside a list.

Structure:

[

[elements],
[elements],
----------,
----------,
[elements]

]

Example 1:

In this example, we will convert list of lists that contains students data to pandas dataframe.

#import pandas
import pandas

#create list of lists 
students=[
          ['IT-7058','vignan','sravan'],
          ['IT-7059','vignan','jyothika'],
          ['IT-7072','vignan','harsha'],
          ['IT-7015','vignan','keshav'],
]

#display the list
print(students)

#convert into pandas dataframe
data=pandas.DataFrame(students)

#display the dataframe
print(data)

Output:

In the above code, we create 4 lists of students data in a list and convert into dataframe.

[['IT-7058', 'vignan', 'sravan'], ['IT-7059', 'vignan', 'jyothika'], ['IT-7072', 'vignan', 'harsha'], ['IT-7015', 'vignan', 'keshav']]
         0       1         2
0  IT-7058  vignan    sravan
1  IT-7059  vignan  jyothika
2  IT-7072  vignan    harsha
3  IT-7015  vignan    keshav

We can provide columns and indices by using columns and index parameters, they can be taken through list.

 

Example 2:

In this example, we will convert list of lists that contains students data to pandas dataframe and provide column names and row names (indices).

#import pandas
import pandas

#create list of lists 
students=[
          ['IT-7058','vignan','sravan'],
          ['IT-7059','vignan','jyothika'],
          ['IT-7072','vignan','harsha'],
          ['IT-7015','vignan','keshav'],
]

#display the list
print(students)

#convert into pandas dataframe
data=pandas.DataFrame(students)

#display the dataframe
print(data)

Output:

In the above code, we create 4 lists of students data in a list and convert into dataframe by providing columns - ['id','college','name'] and rows - ['s1','s2','s3','s4'].

[['IT-7058', 'vignan', 'sravan'], ['IT-7059', 'vignan', 'jyothika'], ['IT-7072', 'vignan', 'harsha'], ['IT-7015', 'vignan', 'keshav']]
         id college      name
s1  IT-7058  vignan    sravan
s2  IT-7059  vignan  jyothika
s3  IT-7072  vignan    harsha
s4  IT-7015  vignan    keshav

Scenario -2 : Convert list of tuples to pandas DataFrame

Here, we are converting collection of tuples to pandas dataframe.

These collection is placed inside a list. tuple is similar to list that is represented by ().

Structure:

[

(elements),
(elements),
----------,
----------,
(elements)

]

Example :

In this example, we will convert list of tuples that contains students data to pandas dataframe and provide column names and row names (indices).

#import pandas
import pandas

#create list of tuples 
students=[
          ('IT-7058','vignan','sravan'),
          ('IT-7059','vignan','jyothika'),
          ('IT-7072','vignan','harsha'),
          ('IT-7015','vignan','keshav'),
]

#display the list
print(students)

#convert into pandas dataframe
data=pandas.DataFrame(students,columns=['id','college','name'],index=['s1','s2','s3','s4'])

#display the dataframe
print(data)

Output:

In the above code, we create 4 tuples of students data in a list and convert into dataframe by providing columns - ['id','college','name'] and rows - ['s1','s2','s3','s4'].

[('IT-7058', 'vignan', 'sravan'), ('IT-7059', 'vignan', 'jyothika'), ('IT-7072', 'vignan', 'harsha'), ('IT-7015', 'vignan', 'keshav')]
         id college      name
s1  IT-7058  vignan    sravan
s2  IT-7059  vignan  jyothika
s3  IT-7072  vignan    harsha
s4  IT-7015  vignan    keshav

Scenario -3 : Convert multiple lists to pandas DataFrame

Here, we are converting indiviual lists to pandas dataframe by zipping the lists using zip() function

 

Syntax:

pandas.DataFrame(zip(list1,;ist2,.,list n))

Example :

In this example, we will create three lists of 2 values each  that contains students data to pandas dataframe and provide column names and row names (indices).

#import pandas
import pandas

#create 3 lists
id=[70,45]
name=['priya','jaya']
city=['hyd','patna']


#convert into pandas dataframe
data=pandas.DataFrame(zip(id,name,city),columns=['id','name','city'],index=['A','B'])

#display the dataframe
print(data)

Output:

In the above code, we specified column names as - ['id','name','city'] and indices as - ['A','B']

   id   name   city
A  70  priya    hyd
B  45   jaya  patna

 


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Jun 14,2024  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!