Convert List to dataframe in PythonIn 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 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-
Using List with Index and Column NamesIn 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-
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-
Using Multidimensional ListIn 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-
Using Multidimensional List with Column and Data TypeIn 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-
Using Lists in the DictionaryFinally, 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-
ConclusionIn this tutorial, we came across some interesting methods of converting a list to a dataframe in Python. Next TopicConvert String to Float in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India