In today’s Python data analysis tutorial we will how to make a list from Pandas objects.
We’ll look into several use cases:
- Convert one DataFrame column to a list.
- Get multiple column values into a list.
- Create a list from an entire DataFrame.
Create Pandas example data
Below is a short Python snippet to create a simple DataFrame that you can use to follow along this example.
import pandas as pd
city = ['Tokyo', 'Paris', 'Osaka','Sidney']
office = ['West', 'West', 'South', 'East']
sales = [110,103,145,190]
offices = dict(city=city, office=office, revenue=sales)
revenue = pd.DataFrame (offices)
print(revenue.head())
Here’s our example data:
city | office | revenue | |
---|---|---|---|
0 | Tokyo | West | 110 |
1 | Paris | West | 103 |
2 | Osaka | South | 145 |
3 | Sidney | East | 190 |
Convert a Series to a list
To make a list out of a DataFrame column, we can simply use the Series to_list() method as shown in the snippet below.
city_list = revenue['city'].to_list()
print(city_list)
And the result will be:
['Tokyo', 'Paris', 'Osaka', 'Sidney']
You can obviously validate the we indeed create a list by using the type function:
type(city_list)
Make a list from multiple columns
In this case, we’ll first define a subset of columns that we’ll slice into a list; then we’ll call the DataFrame.values.tolist() method.
# multiple columns to list
cols = ['city', 'office']
revenue[cols].values.tolist()
Convert entire DataFrame to list
In a similar fashion we are able to get the entire DataFrame into our Python list. Code is shown below:
df_list = revenue.values.tolist()
print(df_list)
The result is obviously a list of lists containing all the DataFrame rows. Each is exported as a list.
[['Tokyo', 'West', 110], ['Paris', 'West', 103], ['Osaka', 'South', 145], ['Sidney', 'East', 190]]
You can now go the other way around and create a DataFrame from the list of lists by running the following code:
revenue2 = pd.DataFrame(df_list, columns = ['city', 'office', 'revenue'])
print(revenue2.head())
DataFrame row to list
It is also easy to save a specific DataFrame row to a Pyhon list. The code below captures the first Dataframe row values into a list.
revenue.loc[0].to_list()
I can also make a list out of several DataFrame rows by slicing the DataFrame or using the loc or iloc indexers:
revenue.loc[1:3].values.tolist()
# or using iloc
revenue.iloc[1:3].values.tolist()