What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

6 Ways to Convert List to Dataframe in Python

  • Mar 01, 2021
  • 4 Minute Read
6 Ways to Convert List to Dataframe in Python

In this article, we will understand what lists and data frames are. We will also study different ways to convert the list to the data frame in python programming. This also answers how to create a pandas data frame from the list in python. So, let’s get started!

What is a List?

The list is the most important data type in python programming. In Python, the list is written as the list of commas separated values inside the square bracket. The most important advantage of the list is the elements inside the list are not compulsorily be of the same data type along with negative indexing. Also, all the operation of the string is similarly applied on list data type such as slicing, concatenation, etc. Also, we can create a nested list i.e. list containing another list.

For Example

# creating a list of items with different data types

sample_list = [10,"favtutor",['A','B']]

print(sample_list)

 

Output

[10, 'favtutor', ['A', 'B']]

 

What is a Data Frame?

Pandas is a software library written for the Python programming language for data manipulation and analysis. Pandas Dataframe is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). A data frame could be a two-dimensional data structure, i.e., knowledge is aligned in a very tabular fashion in rows and columns. Pandas Dataframe consists of 3 principal elements, the data, rows, and columns.

For Example

import pandas as pd

# list of strings

lst = ['fav', 'tutor', 'coding', 'skills']
df = pd.DataFrame(lst)
print(df)

 

Output

       0
0     fav
1   tutor
2  coding
3  skills

 

Convert List to DataFrame in Python

There are many ways to create a data frame from the list. We will look at different 6 methods to convert lists from data frames in Python. Let us study them one by one with an example:

1) Basic method

This is the simplest method to create the data frames from the list.

For example

# import pandas as pd 

import pandas as pd 
# list of strings 
lst = ['fav', 'tutor', 'coding', 'skills']
# Calling DataFrame constructor on list 
df = pd.DataFrame(lst) 
print(df) 

 

Output

       0
0     fav
1   tutor
2  coding
3  skills

 

2) Using a list with index and column names

We can create the data frame by giving the name to the column and index the rows

For example

# import pandas as pd 

import pandas as pd 
# List1 
lst = [['apple', 'red', 11], ['grape', 'green', 22], ['orange', 'orange', 33], ['mango', 'yellow', 44]] 
df = pd.DataFrame(lst, columns =['Fruits', 'Color', 'Value'], dtype = float) 
print(df) 

 

Output

  tutorial
1      fav
2    tutor
3   coding
4   skills

 

3) Using zip() function

We can create the data frame by zipping two lists.

For example

 # import pandas as pd 

import pandas as pd 
# list of strings 
lst1 = ['fav', 'tutor', 'coding', 'skills']
# list of int 
lst2 = [11, 22, 33, 44] 
# Calling DataFrame after zipping both lists, with columns specified 
df = pd.DataFrame(list(zip(lst1, lst2)), columns =['key', 'value']) 
print(df) 

 

Output

      key  value
0     fav     11
1   tutor     22
2  coding     33
3  skills     44

 

4) Creating from the multi-dimensional list

We can create a data frame using multi-dimensional lists.

For example

 # import pandas as pd 

import pandas as pd 
# List1 
lst = [['fav', 11], ['tutor', 22], ['coding', 33], ['skills', 44]] 
df = pd.DataFrame(lst, columns =['key', 'values']) 
print(df) 

 

Output

      key  values
0     fav      11
1   tutor      22
2  coding      33
3  skills      44

 

5) Using a multi-dimensional list with column name

We can create the data frames by specifying the column name and dtype of them.

For example

  # import pandas as pd 

import pandas as pd 
# List1 
lst = [['apple', 'red', 11], ['grape', 'green', 22], ['orange', 'orange', 33], ['mango', 'yellow', 44]] 
df = pd.DataFrame(lst, columns =['Fruits', 'Color', 'Value'], dtype = float) 
print(df) 

 

Output

   Fruits   Color  Value
0   apple     red   11.0
1   grape   green   22.0
2  orange  orange   33.0
3   mango  yellow   44.0

 

6) Using a list in the dictionary

We can create data frames using lists in the dictionary.

For example

# import pandas as pd 

import pandas as pd 
# list of name, degree, score 
n = ["apple", "grape", "orange", "mango"] 
col = ["red", "green", "orange", "yellow"] 
val = [11, 22, 33, 44] 
# dictionary of lists 
dict = {'fruit': n, 'color': col, 'value': val}  
df = pd.DataFrame(dict) 
print(df) 

 

Output

    fruit   color  value
0   apple     red     11
1   grape   green     22
2  orange  orange     33
3   mango  yellow     44

 

Conclusion

While working with a large set of data, it is important to convert the data into a format for easy understanding and operations. Panda data frame provides such an easy format to access the data effectively and efficiently. As we all know that data in python is mostly provided in the form of a List and it is important to convert this list into a data frame.
If you want to practice pandas skills then you can check out Pandas exercises for Beginners.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Shivali Bhadaniya
I'm Shivali Bhadaniya, a computer engineer student and technical content writer, very enthusiastic to learn and explore new technologies and looking towards great opportunities. It is amazing for me to share my knowledge through my content to help curious minds.