Convert List to dataframe in Python
In this tutorial, we will see how we can use a list and convert it into a dataframe in Python.
But before starting with this, let us revise what is the list and what are dataframes?
The list is a data structure in python in which all the elements are enclosed within square brackets.
The example of a list is-
Data frames are the tabular representation of data in the form of rows and columns.
They can be used by importing pandas.
Now let us have a look at the different methods of converting a list to a dataframe in Python.
- Using DataFrame()
- Using list with index and column names
- Using zip()
- Using Multidimensional list
- Using multidimensional list with column and data type
- Using lists in the dictionary
Using pd.DataFrame()
In the first approach we have used the pd.DataFrame() to convert a list.
The following program shows how it can be done-
Output:
0 0 English 1 Hindi 2 Mathematics 3 Science 4 Social Science
Explanation:
It’s time to have a look at the explanation of the above program-
- In the first step we have imported the pandas library.
- After this, we have declared the list that has strings as its values.
- Finally, we have passed this list in DataFrame() and displayed the output.
Using List with Index and Column Names
In the second method, we will create a dataframe that has an index value and a column name.
The program given below illustrates the same.
Output:
Subjects i English ii Hindi iii Mathematics iv Science v Social Science
Explanation:
Now it’s time to understand the above program-
- In the first step we have imported the pandas library.
- After this, we have declared the list that has strings as it’s values.
- Finally, we have passed this list in DataFrame() with a list of index values and the column name.
- On executing the program, it displays the desired output.
Using zip()
In this method we have used zip().
The following program shows how it can be done-
Output:
Subjects Code 0 English 20 1 Hindi 21 2 Mathematics 22 3 Science 23 4 Social Science 24
Explanation:
It’s time to have a look at the explanation of the above program-
- In the first step, we have imported the pandas library.
- After this, we have declared the list that has strings as its values and another list contains the index values.
- Finally, we have passed the list_values and list_index in zip inside DataFrame() with a list of index values and the column name.
- On executing the program, it displays the desired output.
Using Multidimensional List
In this method we will see how a multidimensional list can be used for conversion.
The program given below illustrates the same.
Output:
Subject Name Subject Code 0 English 4101 1 Hindi 4102 2 Science 4103 3 Mathematics 4104 4 Computer 4105
Explanation:
Now it’s time to understand the above program-
- In the first step we have imported the pandas library.
- After this, we have declared the list contains different lists and each list has a string and an integer value.
- Finally, we have passed the list_values in pd.DataFrame() with a list of column names.
- On executing the program, it displays the desired output.
Using Multidimensional List with Column and Data Type
In this approach, we will see a slight variation of the above program.
The following program shows how it can be done-
Output:
First_Name Last_Name Age 0 Colin Lassiter 46.0 1 James Gomez 24.0 2 Sara Charles 34.0 3 Raven Stewart 24.0 4 Oliver Osment 21.0
Explanation:
It’s time to have a look at the explanation of the above program-
- In the first step, we have imported the pandas library.
- After this, we have declared the list contains different lists and each list has two string values(first name and last name) and an integer value(age).
- Finally, we have passed the list_values in DataFrame() with a list of column names and the data type.
- On executing the program, it displays the desired output.
Using Lists in the Dictionary
Finally, in the last method we will see how lists can be used with dictionaries and convert the list into a dataframe.
The program given below illustrates the same.
Output:
First Name Last_Name Age 0 Colin Lassiter 46 1 James Gomez 24 2 Sara Charles 34 3 Raven Stewart 24 4 Oliver Osment 21
Explanation:
Now it’s time to understand the above program-
- In the first step, we have imported the pandas library.
- After this, we have declared three lists, namely f_name, l_name, and age.
- In the next step, we have used these lists as values for the keys of the dictionary.
- Finally, we have passed dict in DataFrame().
- On executing the program, it displays the desired output.
Conclusion
In this tutorial, we came across some interesting methods of converting a list to a dataframe in Python.