AppDividend
Latest Code Tutorials

How To Convert Python List To Dataframe Example

0

Python Pandas.dataframe() is an inbuilt function that takes a list as an argument and converts Python list to dataframe. Python pandas library provides a constructor of DataFrame to create a Dataframe by passing objects

Python list to dataframe

See the following constructor of DataFrame create a Dataframe from a list by passing objects.

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.

Let’s use this to convert lists to dataframe object from lists.

Basic example

See the following code.

# 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 that 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 appear as well as indexes.

Convert Python list to dataframe using zip()

Python zip() is the inbuilt function that returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together. Then the second item 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 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

Create DataFrame from lists of tuples

Just like a list of lists, we can 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 dataframe from list of tuples and skip specific columns

Pandas dataframe.from_records() is an inbuilt function that has a parameter called exclude, which takes a list of columns that 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

Python pandas provide a constructor that can convert Python single list, multi-dimensional list, tuples to Dataframe with columns and indexes, and without columns and indexes.

See also

Python list to json

Python list to dictionary

Python list contains

Python string to list

Python datetime to string

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.