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 is list and data frames. 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 the list?

The list is the most important data type in python language. In Python language, 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,"sid",['A','B']]

 print(sample_list)

Output:

 [10, 'sid', ['A', 'B']]

What is the 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 = ['apple', 'grape', 'orange', 'mango']

 df = pd.DataFrame(lst)

 print(df)

Convert List to DataFrame in Python

There are many ways to create a data frame from the list. We will look at 6 methods to create 6 ways 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 = ['apple', 'grape', 'orange', 'mango']

 # Calling DataFrame constructor on list 

 df = pd.DataFrame(lst) 

 df 

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 

 # list of strings 

 lst = ['apple', 'grape', 'orange', 'mango']

 # Calling DataFrame on list with indices and columns specified 

 df = pd.DataFrame(lst, index =['1', '2', '3', '4'], 

                       columns =['Fruits']) 

 df 

3) Using zip() function: We can create the data frame from zipping two lists.

For example:

 # import pandas as pd 

 import pandas as pd 

 # list of strings 

 lst1 = ['apple', 'grape', 'orange', 'mango']

 # 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 =['Name', 'value']) 

df 

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 = [['apple', 11], ['grape', 22], 

   ['orange', 33], ['mango', 44]] 

 df = pd.DataFrame(lst, columns =['Name', 'values']) 

 df 

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) 

 df 

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 = {'name': n, 'color': col, 'value': val}  

 df = pd.DataFrame(dict) 

 df 

So, these are the 6 different ways to create data frames from the list.

Conclusion

Therefore, we understood what a list is and what the data frame in the python language is. We learned 6 different methods to convert data frames from the python list for example.

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.