In this tutorial, we will see how we can use a list and convert it into a dataframe in Python. Show
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. How do I make a list into a column in Python?Use the tolist() Method to Convert a Dataframe Column to a List. A column in the Pandas dataframe is a Pandas Series . So if we need to convert a column to a list, we can use the tolist() method in the Series . tolist() converts the Series of pandas data-frame to a list.
What does Tolist () do in Python?The tolist() function is used to convert a given array to an ordinary list with the same items, elements, or values.
How do I store a list in a DataFrame column?Insert List into Cell Using DataFrame.at() Method. In order to insert the list into the cell will use DataFrame.at() function. For example, I will use the Duration column from the above DataFrame to insert list. at() inserts a list into a specific cell without raising a ValueError.
How do you convert a list into a row in Python?Method 1: Using T function
This is known as the Transpose function, this will convert the list into a row. Here each value is stored in one column. Example: Python3.
|