In this tutorial, we will explore different methods to convert a dataframe to a list in Python. Depending upon how you want the data in the resulting list, there can be different methods. We’ll look at these methods in detail. By the end of this tutorial, you will have a clear understanding of how to convert dataframes to lists and be able to apply this knowledge to your own projects.
Converting Dataframe to a List
When converting the data in a pandas dataframe to a list, there can be multiple ways in which you may want the values in the resulting list to be depending upon your use case. For example,
- Dataframe to a list of all row values.
- Dataframe to a list of all column values.
- Dataframe to a list of dictionaries.
Let’s now look at the above use cases in detail with the help of some examples.
Dataframe to a list of all row values.
The goal here is to change the dataframe into a list of lists with each individual list containing the values of a single row. To convert a dataframe to a list of row values use df.values.tolist()
where df
is the dataframe you want to convert.
Let’s look at an example.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
import pandas as pd # create a dataframe df = pd.DataFrame({'Name': ['Jim', 'Pam', 'Dwight'], 'Age': [25, 26, 28]}) # display the dataframe print(df)
Output:
Name Age 0 Jim 25 1 Pam 26 2 Dwight 28
Here, we created a dataframe with three rows and two columns. The dataframe contains the name and age information of three people.
Let’s now convert the above dataframe to a list of row values.
# dataframe to list result = df.values.tolist() # display the list print(result)
Output:
[['Jim', 25], ['Pam', 26], ['Dwight', 28]]
You can see that the resulting list contains the row values as separate lists.
Dataframe to a list of column values
The goal here is to convert the dataframe to a list of lists with each individual list containing all the values of a single column. Use the following steps to convert a dataframe to a list of column values –
- Create an empty list to store the result.
- Iterate through each column in the dataframe and for each iteration append the list of column values to the above list.
Let’s look at an example. We’ll use the same dataframe as above.
import pandas as pd # create a dataframe df = pd.DataFrame({'Name': ['Jim', 'Pam', 'Dwight'], 'Age': [25, 26, 28]}) # display the dataframe print(df)
Output:
Name Age 0 Jim 25 1 Pam 26 2 Dwight 28
Let’s now create a list of all column values from the above dataframe.
# dataframe to list of column values result = [] for col in df.columns: result.append(df[col].values.tolist()) # display the result print(result)
Output:
[['Jim', 'Pam', 'Dwight'], [25, 26, 28]]
In the above example, we first create an empty list called result
. Then, use a for loop to iterate over each column in the DataFrame df
. For each column, we use the .values.tolist()
method to convert the column values into a list, and append the resulting list of column values to the result
list.
Finally, the result
list is printed to the console using the print()
function.
You can see we get the list of column values.
3) Dataframe to a list of dictionaries
The goal here is to convert the dataframe to a list of dictionaries where each dictionary represents a row. To convert a Pandas DataFrame to a list of dictionaries in Python, you can use the to_dict()
method with the orient
parameter set to 'records'
. Let’s look at an example.
import pandas as pd # create a dataframe df = pd.DataFrame({'Name': ['Jim', 'Pam', 'Dwight'], 'Age': [25, 26, 28]}) # display the dataframe print(df)
Output:
Name Age 0 Jim 25 1 Pam 26 2 Dwight 28
For the above dataframe, let’s create a list of dictionaries.
# convert dataframe to list of dictionaries result = df.to_dict(orient='records') # display the result print(result)
Output:
[{'Name': 'Jim', 'Age': 25}, {'Name': 'Pam', 'Age': 26}, {'Name': 'Dwight', 'Age': 28}]
You can see that the resulting list contains a list of dictionaries with each dictionary containing the respective row values with the column names as keys.
Conclusion
In conclusion, we have learned how to convert a dataframe to a list in Python using three different methods: converting a dataframe to a list of rows, a list of columns, and a list of dictionaries.
Converting a dataframe to a list in Python is a common task in data analysis and can be achieved using different methods depending on the desired output format. By mastering these techniques, you can easily manipulate dataframes in Python and perform various data analysis tasks.
You might also be interested in –