How to convert DataFrame into List using Python?

In this article, we will study ways to convert DataFrame into List using Python.

DataFrame is a two-dimensional data structure. It consists of rows and columns. Data is aligned in tabular fashion. Hence, we can use DataFrame to store the data.

Lists are also used to store data. However, list is a collection that is ordered and changeable. Lists need not be homogeneous always. Hence, it is a powerful tool in python.

Conversion of DataFrame into List using Python

Initially, import pandas into our code. Through pandas we can use DataFrame. Following is the code:

import pandas as pd

Now we will create a DataFrame. It will have certain rows and columns. Look at the following code:

details = {'Name':['Ravi','Shaam','Kajol','Shruti'],
           'Age' :[23,56,43,21]}

df = pd.DataFrame(details)

print(df)

OUTPUT

Name
Age
0
Ravi
23
1
Shaam
56
2
Kajol
43
3
Shruti
21

 

Let us now look at the conversion.

(i) df.values.tolist()

Conversion of dataframe to list is done using “df.values.tolist()”. “df.values” returns values present in the dataframe. “tolist()” will convert those values into list. Let us look the following code:

df.values

OUTPUT

array([['Ravi', 23],
       ['Shaam', 56],
       ['Kajol', 43],
       ['Shruti', 21]], dtype=object)

These are the values present in the dataframe. Let us now convert these values into list.

df = df.values.tolist()
print(df)

OUTPUT

[['Ravi', 23], ['Shaam', 56], ['Kajol', 43], ['Shruti', 21]]

(ii) zip(*df.values)

Conversion of dataframe to list can also take place using one more method. Here, the zip() function takes iterables, aggregates them in a list and returns it. Iterables could be 0 or more.In case we do not pass any parameter, zip returns an empty iterator. The * operator is used in conjunction with zip to unzip the list. Let us look at the following code:

df = [list (i) for i in zip (*df.values)]
print(df)

OUTPUT

[['Ravi', 'Shaam', 'Kajol', 'Shruti'], [23, 56, 43, 21]]

(iii) iloc

If we want to convert only one column to list we can use iloc. ilocĀ selects data by row number. Let us look at the following code:

df = df.iloc[:,1].tolist()
print(df)

OUTPUT

[23, 56, 43, 21]

In the above example, second column named Age is converted into list. This is done using “iloc[:,1]” which selects second column and “tolist()” converts it into list.

(iv) df.columns.values.tolist()

If we want to convert column names to list, we can use “df.columns.values.tolist()”. “df.column.values” will return the column names and “tolist()” will convert them into list. Let’s look at the following code:

df.columns.values.tolist()

OUTPUT

['Name','Age']

Let us look at one more code. This will convert the entire dataframe along with the column names to list.

df = [df.columns.values.tolist()] + df.values.tolist()
print(df)

OUTPUT

[['Name', 'Age'], ['Ravi', 23], ['Shaam', 56], ['Kajol', 43], ['Shruti', 21]]

Thank You.

You may also read: How to select a Random element from a Tuple in Python ?

Leave a Reply

Your email address will not be published. Required fields are marked *