Python List Dataframe Conversion
In this guide, we’ll learn how to take use of Python’s list comprehension and transform it into a dataframe.
But before we get started, let’s go through what the list is and what dataframes are.
In Python, a list is a data structure whose items are denoted by square brackets.
A list as an illustration
is-
-
Colors=[
‘Red’
,
‘Blue’
,
‘Green’
,
‘Orange’
]
Data frames are the rows and columns that make up a data table.
Importing pandas allows for their utilization.
Let’s have a look at the many ways we may transform that list into 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 method, we have utilized the pd.DataFrame() function to transform a list into a data frame.
The following code demonstrates how it is
done-
Product Code:
-
import
pandas as pd
-
#list_values having strings
-
list_values = [
‘English’
,
‘Hindi’
,
‘Mathematics’
,
‘Science’
,
‘Social Science’
]
-
df = pd.DataFrame(list_values)
-
print(df)
0 0 English 1 Hindi 2 Mathematics 3 Science 4 Social Science
Clarification:
Now let’s have a look at why the aforementioned happened.
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
Method two involves the construction of a dataframe with a specified index value and a named column.
The code sample provided here demonstrates
same.
Resulting
-
import
pandas as pd
-
#list_values having strings
-
list_values = [
‘English’
,
‘Hindi’
,
‘Mathematics’
,
‘Science’
,
‘Social Science’
]
-
df = pd.DataFrame(list_values,index = [
‘i’
,
‘ii’
,
‘iii’
,
‘iv’
,
‘v’
], columns = [
‘Subjects’
])
-
print(df)
:
Explanation:
Subjects i English ii Hindi iii Mathematics iv Science v Social Science
Let’s break down the aforementioned, shall we?
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()
Here, we make use of the zip format ().
The following code demonstrates how it is
done-
Resulting
-
import
pandas as pd
-
#list_values having strings
-
list_values = [
‘English’
,
‘Hindi’
,
‘Mathematics’
,
‘Science’
,
‘Social Science’
]
-
list_index = [
20
,
21
,
22
,
23
,
24
]
-
df = pd.DataFrame(list(zip(list_values, list_index)), columns = [
‘Subjects’
,
‘Code’
])
-
print(df)
Explanation:
Subjects Code 0 English 20 1 Hindi 21 2 Mathematics 22 3 Science 23 4 Social Science 24
Now let’s have a look at why the aforementioned happened.
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
We’ll examine the usage of a multidimensional list in a transformational context.
The code provided here is an example of
same.
Resulting
-
import
pandas as pd
-
#list_values having strings
-
list_values = [[
‘English’
,
4101
], [
‘Hindi’
,
4102
], [
‘Science’
,
4103
], [
‘Mathematics’
,
4104
], [
‘Computer’
,
4105
]]
-
df = pd.DataFrame(list_values, columns = [
‘Subject Name’
,
‘Subject Code’
])
-
print(df)
:
Subject Name Subject Code 0 English 4101 1 Hindi 4102 2 Science 4103 3 Mathematics 4104 4 Computer 4105
Clarification:
The moment has come to comprehend the aforementioned.
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 method, the preceding protocol will be slightly modified.
The following code demonstrates how it is
done-
Resulting
-
import
pandas as pd
-
#list_values having strings
-
list_values = [[
‘Colin’
,
‘Lassiter’
,
46
], [
‘James’
,
‘Gomez’
,
24
], [
‘Sara’
,
‘Charles’
,
34
], [
‘Raven’
,
‘Stewart’
,
24
], [
‘Oliver’
,
‘Osment’
,
21
]]
-
df = pd.DataFrame(list_values, columns = [
‘First_Name’
,
‘Last_Name’
,
‘Age’
], dtype =
float
)
-
print(df)
Explanation:
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
Now let’s have a look at why the aforementioned happened.
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
In the final approach, we’ll merge lists and dictionaries by turning the list into a dataframe.
The code provided here is an example of
same.
Resulting
-
import
pandas as pd
-
#list_values having strings
-
f_name = [
‘Colin’
,
‘James’
,
‘Sara’
,
‘Raven’
,
‘Oliver’
]
-
l_name = [
‘Lassiter’
,
‘Gomez’
,
‘Charles’
,
‘Stewart’
,
‘Osment’
]
-
age = [
46
,
24
,
34
,
24
,
21
]
-
dict = {
‘First Name’
:f_name,
‘Last_Name’
:l_name,
‘Age’
:age}
-
df = pd.DataFrame(dict)
-
print(df)
:
Explanation:
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
The moment has come to comprehend the aforementioned.
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.