Pandas library provides a function of DataFrame to create a Dataframe by passing Python objects.
Python list to dataframe
To convert a list to dataframe in Python, use the pd.dataframe() function. Pandas dataframe() is a built-in library function that takes a list as an argument and converts it into a dataframe.
pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
Here data parameter can be a numpy ndarray, dictionary, or another DataFrame.
Also, columns and indexes are for columns and index labels.
# app.py import pandas as pd sabrina = [['Sabrina', 19, 'Morningstar'], ['Harvey', 20, 'Kinkle'], ['Ambrose', 23, 'Spellman']] df = pd.DataFrame(sabrina) print(df)
Output
python3 app.py 0 1 2 0 Sabrina 19 Morningstar 1 Harvey 20 Kinkle 2 Ambrose 23 Spellman
In the above code, first, we have imported the pandas library and then defined a list, and then passed the list into the dataframe constructor. It will return the dataframe.
One thing to note in the above example is that we did not pass any parameter other than the list, like index, columns, etc.
Python list to dataframe with index and column names
Let’s create a dataframe with index and column names from the python list.
See the following code.
# app.py import pandas as pd sabrina = [['Sabrina', 19, 'Morningstar'], ['Harvey', 20, 'Kinkle'], ['Ambrose', 23, 'Spellman']] df = pd.DataFrame(sabrina, index=['x', 'y', 'z'], columns=['FName', 'Age', 'LName']) print(df)
Output
python3 app.py FName Age LName x Sabrina 19 Morningstar y Harvey 20 Kinkle z Ambrose 23 Spellman
In the above code, we have passed indexes = [‘x’, ‘y’, ‘z’] and columns = [‘FName’, ‘Age’, ‘LName’].
So, in the output, we can see that column names and indexes appear.
Using zip() function
Python zip() is a built-in function that returns a zip object, an iterator of tuples where the first element in each passed iterator is paired together. Then the second element in each given iterator is paired together.
See the following code.
# app.py import pandas as pd sabrinaHell = ['Morningstar', 'Kinkle', 'Spellman'] sabrina = [19, 20, 21] listSab = list(zip(sabrina, sabrinaHell)) df = pd.DataFrame(listSab, columns=['Name', 'Age']) print(df)
Output
python3 app.py Name Age 0 19 Morningstar 1 20 Kinkle 2 21 Spellman
In the above example, we have used the python zip() method to create an iterator and then convert that iterator to a list and pass that list to the dataframe constructor.
Creating DataFrame using a multi-dimensional list
See the following code.
# app.py import pandas as pd sabrinaHell = [['Morningstar', 19], ['Kinkle', 20], ['Spellman', 21]] df = pd.DataFrame(sabrinaHell, columns=['Name', 'Age']) print(df)
Output
python3 app.py Name Age 0 Morningstar 19 1 Kinkle 20 2 Spellman 21
Multi-dimensional list with the column name and dtype
Let’s define another parameter called dtype.
# app.py import pandas as pd sabrinaHell = [['Morningstar', 19], ['Kinkle', 20], ['Spellman', 21]] df = pd.DataFrame(sabrinaHell, columns=['Name', 'Age'], dtype = float) print(df)
Output
python3 app.py Name Age 0 Morningstar 19.0 1 Kinkle 20.0 2 Spellman 21.0
Creating a DataFrame from lists of tuples
To create a dataframe from a list of tuples in Python, use the pd.dataframe() constructor and pass the list of tuples in the dataframe constructor to create a dataframe.
See the following code.
# app.py import pandas as pd sabrina = [('Morningstar', 19), ('Kinkle', 20), ('Spellman', 21)] df = pd.DataFrame(sabrina) print(df)
Output
python3 app.py 0 1 0 Morningstar 19 1 Kinkle 20 2 Spellman 21
Both Column & Index labels are default. But we can also provide them as an argument explicitly.
Create a dataframe from the list of tuples and skip specific columns
Pandas dataframe.from_records() is a built-in function with a parameter called exclude, which takes a list of columns we need to skip.
See the following code.
# app.py import pandas as pd sabrina = [('Morningstar', 19, False), ('Kinkle', 20, True), ('Spellman', 21, True)] df = pd.DataFrame.from_records(sabrina, exclude=['Verify'], columns=['Name', 'Age', 'Verify'], index=['x', 'y', 'z']) print(df)
Output
python3 app.py Name Age x Morningstar 19 y Kinkle 20 z Spellman 21
Conclusion
Pandas provide a constructor that can convert Python single list, multi-dimensional list, and tuples to Dataframe with columns and indexes and without columns and indexes.