- Pandas Column to List – Convert a Pandas Series to a List
- Loading a Sample Dataframe
- Pandas Column to List – Pandas tolist Method
- Pandas Series to List – Python list() function
- Pandas Column to List using Numpy
- Conclusion
- Convert Pandas DataFrame Column to List
- Use the tolist() Method to Convert a Dataframe Column to a List
- Use the list() Function to Convert a Dataframe Column to a List
- Related Article — Pandas DataFrame Column
- Related Article — Pandas DataFrame
- Convert Pandas DataFrame Column to List
- Use the tolist() Method to Convert a Dataframe Column to a List
- Use the list() Function to Convert a Dataframe Column to a List
- Related Article — Pandas DataFrame Column
- Related Article — Pandas DataFrame
- Convert a Pandas DataFrame to a List
- Loading a Sample Pandas DataFrame
- Convert a Pandas DataFrame to a List of Lists
- Convert a Pandas Series (Column) to a List
- Convert a Pandas DataFrame to a List of Tuples
- Convert a Pandas DataFrame to a List of Dictionaries
- Conclusion
- Additional Resources
Pandas Column to List – Convert a Pandas Series to a List
In this post, you’ll learn how to convert a Pandas column to a list, enabling you to also convert a Pandas series into a list.
You’ll learn how to use the Pandas .tolist() method, the Python list() function, and the numpy .tolist() method.
Loading a Sample Dataframe
Let’s start off by loading a sample dataframe. Copy the code below if you wanted to follow along with this tutorial.
import pandas as pd df = pd.DataFrame.from_dict( < 'Name': ['Jim', 'Carl', 'Amy', 'Anne'], 'Age': [23, 45, 35, 19], 'Birth City': ['Montreal', 'Paris', 'Toronto', 'Chicago'], 'Gender': ['M', 'M', 'F', 'F'] >) print(df)
This returns the following dataframe:
Name Age Birth City Gender 0 Jim 23 Montreal M 1 Carl 45 Paris M 2 Amy 35 Toronto F 3 Anne 19 Chicago F
Now let’s get started converting our dataframe column to a list!
Learn how to convert a list to a string in Python by checking out my tutorial here: Learn How to Convert a List to a String in Python.
Pandas Column to List – Pandas tolist Method
First, we’ll use the Pandas .tolist() method to convert our Pandas series to a list. The advantage here is that we don’t need to import numpy if we’re not using it for anything else.
The Pandas .tolist() method acts on a Pandas series (column) and converts it to a list.
As an example, let’s take our name column and convert it to a list:
>>> names = df['Name'].tolist() >>> print(names) ['Jim', 'Carl', 'Amy', 'Anne']
We can see here that a list names has been returned which contains the various values in that column.
Pandas Series to List – Python list() function
Similar to the example above, the we can use Python’s built-in list() function to convert a Pandas series to a list.
Instead of appending the function as a method to a Pandas series, what we’ll do is wrap the column in the list() function.
In the below example, let’s convert the Birth City column to a list:
>>> cities = list(df['Birth City']) >>> print(cities) ['Montreal', 'Paris', 'Toronto', 'Chicago']
Similar to using the Pandas .tolist() method, the built-in list() function returns a list from a Pandas column.
Pandas Column to List using Numpy
As the last method covered in this tutorial, you’ll learn how to use the numpy .tolist() method.
We’ll first convert the Pandas series into a numpy array by using the .values attribute. Then we’ll call the .tolist() method to return a list.
For this final example, let’s convert the Age column to a list:
>>> ages = df['Age'].values.tolist() >>> print(ages) [23, 45, 35, 19]
Conclusion
In this post you learned how to convert a Pandas column to a list, allowing you also to convert a Pandas series to a list. You learned how to do this using the Pandas tolist() method, the Python list() function, and the numpy .tolist() method.
To learn more about the Pandas tolist() method, check out the official documentation here. To learn more about the Python list() function, check out the official documentation here.
Convert Pandas DataFrame Column to List
- Use the tolist() Method to Convert a Dataframe Column to a List
- Use the list() Function to Convert a Dataframe Column to a List
This tutorial article will introduce different methods to convert a Pandas DataFrame column to a list, like using the tolist() method in Pandas.
Use the tolist() Method to Convert a Dataframe Column to a List
A column in the Pandas dataframe is a Pandas Series . So if we need to convert a column to a list, we can use the tolist() method in the Series . tolist() converts the Series of pandas data-frame to a list.
In the code below, df[‘DOB’] returns the Series , or the column, with the name as DOB from the DataFrame.
The tolist() method converts the Series to a list.
import pandas as pd df=pd.DataFrame([ ['James', '1/1/2014', '1000'], ['Michelina', '2/1/2014', '12000'], ['Marc', '3/1/2014', '36000'], ['Bob', '4/1/2014', '15000'], ['Halena', '4/1/2014', '12000'] ], columns=['Name', 'DOB','Salary']) print("Pandas DataFrame:\n\n",df,"\n") list_of_single_column = df['DOB'].tolist() print("the list of a single column from the dataframe\n", list_of_single_column, "\n", type(list_of_single_column))
Pandas DataFrame: Name DOB Salary 0 James 1/1/2014 1000 1 Michelina 2/1/2014 12000 2 Marc 3/1/2014 36000 3 Bob 4/1/2014 15000 4 Halena 4/1/2014 12000 the list of a single column from the dataframe ['1/1/2014', '2/1/2014', '3/1/2014', '4/1/2014', '4/1/2014']
Use the list() Function to Convert a Dataframe Column to a List
We can also use the list() function to convert a DataFrame column to a list, by passing the DataFrame to the list() function.
We will use the same data as above to demonstrate this approach.
import pandas as pd df=pd.DataFrame([ ['James', '1/1/2014', '1000'], ['Michelina', '2/1/2014', '12000'], ['Marc', '3/1/2014', '36000'], ['Bob', '4/1/2014', '15000'], ['Halena', '4/1/2014', '12000'] ], columns=['Name', 'DOB','Salary']) print("Pandas DataFrame:\n\n",df,"\n") list_of_single_column = list(df['DOB']) print("the list of a single column from the dataframe\n", list_of_single_column, "\n", type(list_of_single_column))
Pandas DataFrame: Name DOB Salary 0 James 1/1/2014 1000 1 Michelina 2/1/2014 12000 2 Marc 3/1/2014 36000 3 Bob 4/1/2014 15000 4 Halena 4/1/2014 12000 the list of a single column from the dataframe ['1/1/2014', '2/1/2014', '3/1/2014', '4/1/2014', '4/1/2014']
Related Article — Pandas DataFrame Column
Related Article — Pandas DataFrame
Convert Pandas DataFrame Column to List
- Use the tolist() Method to Convert a Dataframe Column to a List
- Use the list() Function to Convert a Dataframe Column to a List
This tutorial article will introduce different methods to convert a Pandas DataFrame column to a list, like using the tolist() method in Pandas.
Use the tolist() Method to Convert a Dataframe Column to a List
A column in the Pandas dataframe is a Pandas Series . So if we need to convert a column to a list, we can use the tolist() method in the Series . tolist() converts the Series of pandas data-frame to a list.
In the code below, df[‘DOB’] returns the Series , or the column, with the name as DOB from the DataFrame.
The tolist() method converts the Series to a list.
import pandas as pd df=pd.DataFrame([ ['James', '1/1/2014', '1000'], ['Michelina', '2/1/2014', '12000'], ['Marc', '3/1/2014', '36000'], ['Bob', '4/1/2014', '15000'], ['Halena', '4/1/2014', '12000'] ], columns=['Name', 'DOB','Salary']) print("Pandas DataFrame:\n\n",df,"\n") list_of_single_column = df['DOB'].tolist() print("the list of a single column from the dataframe\n", list_of_single_column, "\n", type(list_of_single_column))
Pandas DataFrame: Name DOB Salary 0 James 1/1/2014 1000 1 Michelina 2/1/2014 12000 2 Marc 3/1/2014 36000 3 Bob 4/1/2014 15000 4 Halena 4/1/2014 12000 the list of a single column from the dataframe ['1/1/2014', '2/1/2014', '3/1/2014', '4/1/2014', '4/1/2014']
Use the list() Function to Convert a Dataframe Column to a List
We can also use the list() function to convert a DataFrame column to a list, by passing the DataFrame to the list() function.
We will use the same data as above to demonstrate this approach.
import pandas as pd df=pd.DataFrame([ ['James', '1/1/2014', '1000'], ['Michelina', '2/1/2014', '12000'], ['Marc', '3/1/2014', '36000'], ['Bob', '4/1/2014', '15000'], ['Halena', '4/1/2014', '12000'] ], columns=['Name', 'DOB','Salary']) print("Pandas DataFrame:\n\n",df,"\n") list_of_single_column = list(df['DOB']) print("the list of a single column from the dataframe\n", list_of_single_column, "\n", type(list_of_single_column))
Pandas DataFrame: Name DOB Salary 0 James 1/1/2014 1000 1 Michelina 2/1/2014 12000 2 Marc 3/1/2014 36000 3 Bob 4/1/2014 15000 4 Halena 4/1/2014 12000 the list of a single column from the dataframe ['1/1/2014', '2/1/2014', '3/1/2014', '4/1/2014', '4/1/2014']
Related Article — Pandas DataFrame Column
Related Article — Pandas DataFrame
Convert a Pandas DataFrame to a List
In this post, you’ll learn how to convert a Pandas DataFrame to a list, including a list of lists, a list of tuples, and a list of dictionaries. Being able to convert a Pandas DataFrame to different formats allows you to work with different libraries that may not accept Pandas DataFrames. Pandas provides you with a large number of options for converting your data to other formats.
By the end of this tutorial, you’ll have learned:
- How to convert a Pandas DataFrame to a list of lists
- How to convert a Pandas DataFrame column (or Series) to a list
- How to convert a Pandas DataFrame to a list of tuples
- How to convert a Pandas DataFrame to a list of dictionaries
Loading a Sample Pandas DataFrame
Below, you’ll find code to load a sample Pandas DataFrame that you can use to follow along with. If you have your own dataset, feel free to use that. However, your results will of course vary.
# Loading a Sample Pandas DataFrame import pandas as pd df = pd.DataFrame(< 'Name': ['Nik', 'Kate', 'Evan', 'Kyra'], 'Age': [33, 34, 43, 44], 'Score': [90, 95, 93, 92] >) print(df) # Returns: # Name Age Score # 0 Nik 33 90 # 1 Kate 34 95 # 2 Evan 43 93 # 3 Kyra 44 92
You can see from the code above that you have loaded a Pandas DataFrame with three columns and four records. The dataset is deliberately simple, in order for you to see the entire process.
Convert a Pandas DataFrame to a List of Lists
In this section, you’ll learn how to convert a DataFrame to a list of lists. Pandas DataFrames have an attribute, .values , which contains all of the values in DataFrame represented as a NumPy array. NumPy arrays have a method available, .tolist() , which converts an array to a list.
Let’s see what this looks like:
# Convert a Pandas DataFrame to a List of Lists import pandas as pd df = pd.DataFrame(< 'Name': ['Nik', 'Kate', 'Evan', 'Kyra'], 'Age': [33, 34, 43, 44], 'Score': [90, 95, 93, 92] >) values = df.values.tolist() print(values) # Returns: # [['Nik', 33, 90], ['Kate', 34, 95], ['Evan', 43, 93], ['Kyra', 44, 92]]
In the code block above, we accessed the values of the DataFrame and then applied the .tolist() method. This returns a list of lists, where each sublist is a record in the DataFrame.
In the following section, you’ll learn how to convert a Pandas Series to a list.
Convert a Pandas Series (Column) to a List
In order to convert a Pandas Series to a list, you can either apply the .tolist() to a Pandas Series or pass the Series into the list() function. These methods accomplish the same thing! Let’s take a look at how we can use either of these methods to convert the ‘Age’ column to a list:
# Convert a Pandas Series to a List import pandas as pd df = pd.DataFrame(< 'Name': ['Nik', 'Kate', 'Evan', 'Kyra'], 'Age': [33, 34, 43, 44], 'Score': [90, 95, 93, 92] >) list1 = df['Age'].values.tolist() list2 = list(df['Age']) print('list1 contains: ', list1) print('list2 contains: ', list2) # Returns: # list1 contains: [33, 34, 43, 44] # list2 contains: [33, 34, 43, 44]
We can see that both methods explained above return the same list of values. The first method, using df[‘Age’].value.tolist() can be more intentional and descriptive of what you’re hoping to accomplish.
In the following section, you’ll learn how to convert a DataFrame to a list of tuples.
Convert a Pandas DataFrame to a List of Tuples
In order to convert a Pandas DataFrame into a list of tuples, you can use the .to_records() method and chain it with the .tolist() method. The .to_records() method converts the DataFrames records into tuple-like containers. When the .tolist() method is then applied, the object is converted to a Python list.
Let’s see what this process looks like:
# Convert a Pandas DataFrame to a List of Tuples import pandas as pd df = pd.DataFrame(< 'Name': ['Nik', 'Kate', 'Evan', 'Kyra'], 'Age': [33, 34, 43, 44], 'Score': [90, 95, 93, 92] >) list_of_tuples = df.to_records().tolist() print(list_of_tuples) # Returns: # [(0, 'Nik', 33, 90), (1, 'Kate', 34, 95), (2, 'Evan', 43, 93), (3, 'Kyra', 44, 92)]
In the next section, you’ll learn how to convert a DataFrame to a list of dictionaries.
Convert a Pandas DataFrame to a List of Dictionaries
In order to convert a Pandas DataFrame to a list of dictionaries, you can use the .to_dict() method. The method provides a number of different ways in which you can convert your Pandas DataFrame.
By passing in ‘records’ into the method, the DataFrame is converted into a list of dictionaries. This can be helpful when moving the DataFrame into other languages, given how closely the format matches a JSON-style format.
Let’s see how we can accomplish this:
# Convert a Pandas DataFrame into a List of Dictionaries import pandas as pd df = pd.DataFrame(< 'Name': ['Nik', 'Kate', 'Evan', 'Kyra'], 'Age': [33, 34, 43, 44], 'Score': [90, 95, 93, 92] >) list_of_dicts = df.to_dict('records') print(list_of_dicts) # Returns: # [, , , ]
Conclusion
In this post, you learned how to convert a Pandas DataFrame into a list. This process can be helpful when moving a dataset between libraries (or even languages). You first learned how to convert a DataFrame to a list, then how to convert a Series to a list. You also learned how to convert a DataFrame to a list of tuples and a list of dictionaries.
Additional Resources
To learn more about related topics, check out the tutorials below: