Python convert DataFrame to list

In this Python tutorial, we will learn how to convertĀ PandasĀ DataFrame to a list using Python. Also, we will cover these topics.

  • Python convert Dataframe to list of dictionaries
  • Python convert Dataframe to list of lists
  • Python convert Dataframe to list of tuples
  • Python convert Dataframe to 2d list
  • Python convert Dataframe to list of scala
  • Python convert Dataframe to nested list Python
  • Python convert Dataframe to list of strings
  • Python convert Dataframe to list of json
  • Python convert Dataframe to list of Columns
  • Python convert Dataframe to list of Series
  • Python convert Dataframe to list without index
  • Python convert list to pandas dataframe with header

Python convert DataFrame to list

  • In this Programm, we will discuss how to convert Pandas DataFrame to list in Python.
  • In Python, a DataFrame is like a data structure that is in the form of rows and columns and if you want to store the data then you can easily use the concept of DataFrame. While Python lists are also used to store data.
  • Let us discuss and see how to convert DataFrame to list. There are many methods to convert DataFrame to list.
    • By using df.values.tolist()
    • By using iloc method
    • By using df.Series.tolist()

Note: First, we have to create a DataFrame by using the Pandas module in Python. To create a DataFrame, First, we need specific data in which list has been store so now we will create a dictionary in which keys elements are column names and the elements of the value are the lists of values.

Also, read How to Set Column as Index in Python Pandas

By using df.values.tolist()

This method will help the user to convert data items of a DataFrame into a list. Let’s use this method in an example and check how we will use the tolist() function in Pandas.

Example:

import pandas as pd

employee_info = {'emp_name':['Chris', 'Adam', 'Oliva', 'Elijah' ] ,
		'emp_id': [68,39,118,724] }

new_df = pd.DataFrame(employee_info)

d = new_df['emp_name'].tolist()
print("Convert dataframe to list:",d)

In the above code, we have used pd.Dataframe() function to declare the DataFrame from the dictionary and then use the tolist() function to convert the Dataframe to a list containing all the rows of column ’emp_name’. Once you will print ‘d’ then the output will display in the form of a list.

Here is the execution of the following given code

Python Convert Dataframe to list
Python Convert Dataframe to list

By using iloc method

Here we can see how to convert a column to a list by using the iloc method. Python provides a method DataFrame. iloc() and this method is used when the index label of the DataFrame and will always return an integer location.

Syntax:

Here is the Syntax of the DataFrame.iloc method

Pandas.DataFrame.iloc[]

Note: The iloc choose data by row number

Source Code:

import pandas as pd

new_dictionary = {
    'Country_name': ['Germany', 'Australia', 'France','England'],
    'Values': [843,290,179,926],
    'City':['paris','Japna','Kenya','Newzealand']
    
}
z = pd.DataFrame(new_dictionary)
print(z)
m = z.iloc[:, 0].tolist()
u = z.iloc[:, 1].tolist()
w = z.iloc[:, 2].tolist()
print("list of val:",m)
print("list of val:",u)
print("list of val:",w)

Here is the execution of the following given code

Python Convert Dataframe to list
Python Convert Dataframe to list

This is how to convert a DataFrame into a list.

Read: Python convert dictionary to an array

Python convert Dataframe to list of dictionaries

  • Let us see how to convert a DataFrame to a list of dictionaries by using the df.to_dict() method.
  • In Python DataFrame.to_dict() method is used to covert a dataframe into a list of dictionaries. Let’s take an example and create a dataframe first with three columns ‘student_name’, ‘student_id’ and ‘Student_address’.

Syntax:

Here is the Syntax of df.to_dict

DataFrame.to_dict(orient='dict',into)

Note: In this example, we have passed ‘record’ as an argument in the df.to_dict() method. In Python, the record orientation defines which type to convert columns and each column is made a dictionary.

Source Code:

import pandas as pd

df = pd.DataFrame({'student_name': ['Hayden', 'Mathew','James','George'],
                   'student_id': [854,778,290,160],
                  'Student_address':['China', 'Moscow','Russia','France']})
print("Display dataframe:",df)
b = df.to_dict('records')
print("Convert Dataframe to list of dictionaries:",b)

In the above code, we have to change the orient to records and it will return the list of dictionaries, and each dictionary stores the separate rows.

You can refer to the below Screenshot

Python Convert DataFrame to list of dictionaries
Python Convert DataFrame to list of dictionaries

This is how to convert a DataFrame into a list of dictionaries.

Read: Python convert dictionary to list

Python convert DataFrame to list of lists

  • In this program, we will see how to convert a DataFrame into a list of lists.
  • To perform this particular task we can use the concept of the tolist() function. In Python values.tolist() method will help the user to convert data items of a DataFrame into a list.

Example:

import pandas as pd

df = pd.DataFrame([[85,28,191],[924,167,335]])
m = df.values.tolist()
print("Convert Dataframe to list of lists:",m)

In the above code First, we have imported a Pandas library and then create a dataframe ‘df’ which assigns a tuple pair list. Now if we want to get a list of lists with each element in the list. Once you will print ‘m’ then the output will display in the form of a list of lists.

Here is the Output of the following given code

Convert DataFrame to list of lists
Convert DataFrame to list of lists

Read: Could not convert string to float Python

Python convert DataFrame to list of tuples

  • Here we can see how to convert a Pandas dataframe into a list of tuples.
  • By using the Pandas.to_records() method we can easily perform this task, and this method will help the user to convert the dataframe to a NumPy record array and within this function, we have passed index as a parameter. The index will be considered as the first field of the record NumPy array and by default its value is True.

Syntax:

Here is the Syntax of Pandas.to_records

DataFrame.to_records(
                     index=True,
                     column_dtypes=None,
                     index_dtypes=None
                    )

Source Code:

import pandas as pd

new_data = pd.DataFrame({'George': [72,56,39], 'Micheal': [623,745,845]})
new_val = new_data.to_records(index=False)
output = list(new_val)
print("Convert Dataframe to list of tuples:",output)

In the above program, we have also applied the list(iterable) method to convert tuple packaged into a list.

You can refer to the below Screenshot

Python DataFrame to list of tuples
Python DataFrame to list of tuples

Read: Convert string to float in Python

Python convert DataFrame to list 2d list

  • Here we can see how to Convert a DataFrame into a list of 2d lists.
  • By using the Concept of to_numpy().tolist method we can easily convert Pandas DataFrame into a list of 2d lists, by converting either each row or column. To do this first we have to create a list of tuples and then create a dataframe object ‘new_val’.
  • In Python to_numpy() function is used to return a ndarray which contains values in the form of a given series or index.

Syntax:

DataFrame.to_numpy(
                   dtype=None,
                   copy=False,
                   na_value=Nodefault.no_default
                  )

Source Code:

import pandas as pd

Country_name = [('Newzealand', 34, 'Switzerland', 155),
            ('Germany', 31, 'Australia', 177.5),
            ('France', 16, 'Paris', 81),
            
        ]
new_val = pd.DataFrame(Country_name)
result = new_val.to_numpy().tolist()
new_output = new_val.to_numpy()
print(new_output)

In the above program, after fetching each row as a list and declare a list of these lists

Here is the execution of the following given code

Python Convert DataFrame to list 2d list
Python Convert DataFrame to list 2d list

Read: Convert float to int Python

Python convert DataFrame to nested list Python

  • In this program, we will see how to convert a Pandas dataframe to Python nested list.
  • In Python for creating a nested list to dataframe we have to use tolist() and for loop method and each inner list contains all the columns of a specified row.

Source Code:

import pandas as pd

employee_data = {'emp_name':['Lucas', 'William', 'Liam', 'Noah' ] ,'emp_id': [623,884,167,134] }

df = pd.DataFrame(employee_data)
new_output=[]

for new_col in df.columns:

	new_val = df[new_col].tolist()
	

	new_output.append(new_val)
print(new_output)

In the above code first, we have created a dictionary ’employee_data’ for storing data items and then create a data frame to use the Pandas module. Now initialize an empty list and iterate through the columns of the DataFrame.

You can refer to the below Screenshot

Convert DataFrame to nested list Python
Convert DataFrame to nested list Python

Read: How to Convert DateTime to UNIX timestamp in Python

Python convert DataFrame to list of strings

  • In this Programm, we will see how to convert a DataFrame into a list of strings.
  • In Python tolist() function is used to convert a DataFrame to a list and this function can be used in the below example and convert the required DataFrame into a list of strings. This function always returns a list of the values.

Syntax:

Here is the Syntax of the tolist() function

df.values.tolist()

Example:

import pandas as pd

student_info = {'stu_name':['Hayden', 'Gilchrist', 'Adam', 'James' ] ,
		'stu_id': [167,992,489,119] }

new_data = pd.DataFrame(student_info)

new_val = new_data['stu_name'].tolist()

print("Convert DataFrame to list of strings:",new_val)

In this Program, we have created a dictionary ‘student_info’ which store the data in the form of keys and value. In this example, we will Consider key elements as column names and then create a dataframe ‘new_data’. Now to convert a DataFrame to a list that contains string elements we can use the function tolist().

Here is the execution of the following given code

Convert DataFrame to list of strings
Convert DataFrame to list of strings

Read: How to convert dictionary to JSON in Python

Python convert DataFrame to list of JSON

  • Here we can see how to convert a DataFrame to list of JSON string.
  • In this example we can use the concept of df.to_json this method will help the user to convert a DataFrame into a list of JSON. In Python, the JSON depends on the orient parameter and by default, its value is ‘record’.

Syntax:

Here is the Syntax of the DataFrame.to_json() method

DataFrame.to_json(
                  path_or_buf=None,
                  orient=None,
                  date_format=None,
                  double_precision=10,
                  force_ascii=True,
                  date_unit='ms',
                  default_handler=None,
                  Lines=False,
                  Compression='infer',
                  index=True,
                  Storage_options=None
                 )
  • It consists of a few parameters
    • path_or_bf: This parameter is specified to set a file path and if it is not given then the output is returned as a string.
    • Orient: This parameter specifies the indication of string format and in the below, we have passed ‘records’ format as a parameter and the list is like [{column ->value}].
    • data_format: By default its value is None and it always depends on the orient. For orient=’table’,the default is ‘iso’.
    • double_precision: By default, its value is integer 10.
    • force_ascii: This parameter specifies the encoded string. By default it passes ‘True’ as a parameter.
    • date_unit: It is used when we have to convert time unit to encode.
    • default_handler: By default its value is None.
    • lines: This parameter checks if ‘orient’ is ‘records’ then JSON format is used and by default its value is ‘False’.
    • Compression: {‘infer’,’gzip’,’bz2′,’zip’}.
    • Index: By default the index value is ‘False’ and it specifies the index value in the JSON string.
    • Storage_options: dict and it is an optional parameter.

Example:

import pandas as pd

df = pd.read_json('[{"China" : 840, "Australia" : 197}, {"Mexico" : 134, "Russia" : 104}]', orient='records')
new_str = list()
print (df.to_json(orient='records'))

In the above program first, we have imported a pandas module and then create a dataframe in which we have passed JSON format and it depends on the value of orient =’ record’ argument.

You can refer to the below Screenshot

Python Convert DataFrame to list of JSON
Python Convert DataFrame to list of JSON

Read: How to Convert Python string to byte array

Python convert DataFrame to list of Columns

  • In this Program, we will see how to convert a DataFrame into list of columns.
  • In this example we can use the concept of dict.keys() method. In Python, this function returns a list that contains all the keys which are available in the dictionary. In this example, we have considered keys as columns that are ‘stu_name’ and ‘stu_id’.

Source Code:

import pandas as pd

student_info = {'stu_name':['Hayden', 'Gilchrist', 'Adam', 'James' ] ,
		'stu_id': [732,881,589,149] }
d= student_info.keys()
print(d)

In the above program, we have created a dictionary ‘student_info’, and then use the dict.keys() method will help the user to convert a dataframe into a list.

Here is the execution of the following given code

Python Convert DataFrame to list of Columns
Python Convert DataFrame to list of Columns

An alternative method to check how to convert a DataFrame into a list of columns.

In this example, we will read a CSV file into a dataframe and then apply the series.tolist() function to convert a dataframe into a list of columns. In the below code we select the column “china” using a [] operator that always returns a series item.

Syntax:

Here is the Syntax of pandas.Series.tolist()

Series.tolist()

Note: It consists of no parameters and returns a list of the values.

Source Code:

import pandas as pd

info_dt = pd.read_csv("test1.csv")
df = info_dt.head(3)

new_val = df["China"].tolist()

print("Convert dataframe to list of column:",new_val)

Here is the Screenshot and Output code of the CSV file

Python convert DataFrame to list of columns csv
Python convert DataFrame to list of columns CSV

You can refer to the below Screenshot for converting a DataFrame into a list of columns

Python Convert DataFrame to list of Columns
Python Convert DataFrame to list of Columns

Read: How to convert a String to DateTime in Python

Python convert DataFrame to list of Series

  • Let us see how to convert a DataFrame into a list of series in Python.
  • To perform this task we can use the concept of values.tolist() method. This function will help the user to convert data items of a dataframe into a list. In the below example first, we have converted a dataframe to a numpy array by using values and then convert it to a list. After that, we have created a new Series with an index from ‘new_dt’.

Source Code:

import pandas as pd

new_dt = pd.DataFrame([[39,55,15,93], [54,39,12,14]], ['Rose', 'Tulip'])
print (pd.Series(new_dt.values.tolist(), index=new_dt.index))

Here is the Screenshot of the following given code

Python Convert DataFrame to list of Series
Python Convert DataFrame to list of Series

Read: How to convert Python degrees to radians

Python convert DataFrame to list without index

  • In this program, we will discuss how to convert a DataFrame into a list without an index in Python.
  • Here we can use the concept of DataFrame.to_numpy() function. In Python, this method returns an object of type NumPy ndarray.

Syntax:

Here is the Syntax of Pandas.DataFrame.to_Numpy

DataFrame.to_Numpy(
                  dtype=None,
                  copy=False,
                  na_value=NoDefault.no_default
                 )
  • It Consists of a few parameters
    • dtype: This parameter is to specify the type of data.
    • Copy: This parameter is used to return a full same copy of a given array and by default, Copy assigns a parameter ‘False’.
    • na_value: This is an optional parameter and by default Pandas return the NA value.

Example:

import pandas as pd

df = pd.DataFrame({'0' : ['82','16','72','118'],
       '1' : ['109','116','179','482'],
       '2' : ['467','915','119','222'],
       '3' : ['655','882','639','193']})
b = df.to_numpy('int').tolist()
print("Convert df to list without index:",b)

In the above code, we have created a nested list from a Pandas DataFrame and then apply the function df.to_numpy() along with the tolist() function. Once you will print ‘b’ then the output will display a list without an index value.

Here is the implementation of the following given code

Python Convert DataFrame to list without index
Python Convert DataFrame to list without index

Read: Python convert tuple to list

Python convert list to Pandas DataFrame to header

  • Let us see how to convert a list into a Pandas DataFrame with header.
  • By using the slicing and Pandas.DataFrame constructor, we can convert a list into a dataframe. In this example, we have mentioned data in the given list and columns names as separate lists.

Source Code:

import pandas as pd
new_info = [['stu_name','stu_age'],
              ['George', 45],
                ['Micheal', 22],
                ['Hyaden', 18],
                   ['Chris', 26],
                ['Potter', 34]]

new_val = pd.DataFrame(new_info[1:],columns=new_info[0])
print(new_val)

In the above program, we have specified the first index for pd.DataFrame() method. Once you will print ‘new_val’ then the Output will display the dataframe that contains elements in the form of rows and columns along with the header.

You can refer to the below Screenshot

Python Convert list to Pandas DataFrame to header
Python Convert list to Pandas DataFrame to a header

An alternative way to check how to convert a list to Pandas DataFrame to a header.

By using the zip() method we can perform this particular task. In Python, the zip() method takes iterable as a tuple and also contains elements of the same index.

Example:

import pandas as pd

new_lis1 = ['Seria','oliva','Elijah','Jefer','William','john','potter']

new_lis2 = [62,19,35,92,45,91,29]

df = pd.DataFrame(list(zip(new_lis1, new_lis2)),
			columns =['emp_name', 'emp_id'])

print("Convert list to dataframe with header:",df)

In the above example, we have created two lists ‘new_lis1’ and ‘new_lis2’. Now we want to combine these two lists and create a DataFrame by calling a pd.DataFrame constructor in which column names have been specified.

Here is the execution of the following given code.

Python Convert list to Pandas DataFrame to header
Python Convert list to Pandas DataFrame to a header

Another approach to check how to convert a python list into pandas DataFrame.

By using a multidimensional list we can create a Pandas DataFrame.

Source Code:

import pandas as pd
	
new_val = [['z', 119], ['u', 821],
	['p', 654], ['t', 701]]
	
df = pd.DataFrame(new_val, columns =['Alphabet', 'Value'])
print("Convert a list to Pandas with header:",df)

Here is the Screenshot of the following given code

Python Convert list to pandas dataframe to header
Python Convert list to pandas dataframe to a header

This is how to create a Pandas DataFrame from lists.

You also like to read the following articles.

In this tutorial, we learned how to convert Pandas DataFrame to a list using Python. Also, we have covered these topics.

  • Python convert Dataframe to list of dictionaries
  • Python convert Dataframe to list of dictionaries
  • Python convert Dataframe to list of lists
  • Python convert Dataframe to list of tuples
  • Python convert Dataframe to 2d list
  • Python convert Dataframe to list of scala
  • Python convert Dataframe to nested list Python
  • Python convert Dataframe to list of strings
  • Python convert Dataframe to list of json
  • Python convert Dataframe to list of Columns
  • Python convert Dataframe to list of Series
  • Python convert Dataframe to list without index
  • Python convert list to pandas dataframe with header