Converting a list to a DataFrame can be very useful for a number of scenarios. In this article, we will study different ways to convert the list to the data frame in Python. This also answers how to create a pandas data frame from the list. But before that, let's revise what is a list and a dataframe?
What is a List?
The list is one of the most important data types of Python. It is written as the list of comma-separated values inside the square brackets. The most important advantage of the list is the elements inside the list are not compulsorily of the same data type along with negative indexing. Also, we can create a nested list i.e. list containing another list. Here is an 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 DataFrame?
Pandas is a software library written for Python programming, useful in data manipulation and analysis. Pandas Dataframe is a two-dimensional tabular data structure with labeled axes (rows and columns). They are amazing when working with data, including indexing, filtering, grouping, merging, reshaping, and more. See the example below:
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
How to Convert List to DataFrame in Python?
As we discussed, DataFrames are used for data manipulation. So, you can easily perform operations on data if the list is converted to a dataframe. Also, a DataFrame showcases the data tabular format that is easier to read than a list. We will now look at 8 different methods to convert lists from data frames in Python. Let us study them one by one with examples:
1) Basic Method
Let's start with the most basic method to solve the problem and make a data frame out of a list.
We can use the DataFrame constructor to convert a list into a DataFrame in Python. It takes the list as input, and then, each inner list represents a row of the DataFrame. This constructor is a class method of the Pandas DataFrame class.
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 & column names
We can create the data frame by giving the name to the column and indexing the rows. Here we also used the same DataFrame constructor as above.
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. The zip() is a built-in function that takes two or more lists as arguments and returns a list where the elements of the same index in the lists are paired together.
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. We used the same DataFrame constructor here. Each inner list represents a row in the following example.
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 multi-dimensional list with column name
We can create the data frames by specifying the column name and dtype of them. Here the columns name is additional row for better understanding.
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. First we create a dictionary of lists and then use the constructor and input the dictionary as the argument.
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
7) Converting nested lists into a dataFrame
You can use the pandas DataFrame constructor and pass the list of lists as the data parameter to convert it to a DataFrame. The columns parameter can also be used to specify the column names. Here's an example:
import pandas as pd new_list = [['Anurag', 'btr001'], ["Bhumika", 'btr002'], ['Chirag', 'btr003']] df = pd.DataFrame(new_list, columns=['Students', 'Roll Number']) print(df)
Output:
Students Roll Number 0 Anurag btr001 1 bhumika btr002 2 chriag btr003
8) Converting mulitple lists into a dataFrame
To turn multiple lists into a DataFrame, you can use the zip function to combine the lists and then pass the result to the pandas DataFrame constructor.
Example:
import pandas as pd std = ['Anurag','bhumika','chriag'] rn = ['btr001','btr002','btr003'] df = pd.DataFrame(list(zip(std, rn)), columns=['Students', 'Roll Number']) print(df)
Output:
Students Roll Number 0 Anurag btr001 1 bhumika btr002 2 chriag btr003
Also learn, how to convert list to string in python.
Conclusion
Now we discussed all the methods to create dataframes from a list in python. While working with a large set of data, this become an important task because dataframe provides such an easy format to access the data effectively and efficiently.