There are cases in which when working with Pandas Dataframes and data series objects you might need to convert those into lists for further processing.
In this post we’ll convert into lists (or list of lists) the following:
- Dataframe columns
- Dataframe rows
- Entire Dataframes
- Data series arrays
Creating your sample Dataframe
Before we get started let’s set the environment and create a simple Dataframe to work with. We’ll convert a Python dictionary containing fictitious information on programming languages and their perceived popularity. This is just for training/learning purposes, as in real live you’ll be importing your data from .csv files (read_csv), JSON, sql databases and so forth.
Paste the following into your favorite Python editor to create the test Dataframe.
Let’s take a look at the data. Run the following:
Here’s the output:
Convert a column/series to list of strings
Let’s go ahead and convert the Language column to a list of strings. Here’s how you can easily do it:
languages = list(data["Language"])
Let’s look at the output:
Python created a list of strings:
Note: you can learn how to work with Python DataFrame columns, using our tutorial on creating one or multiple columns in Pandas and sorting Python DataFrame columns.
Convert a Pandas row to a list
Now we would like to extract one of the dataframe rows into a list. For simplicity let’s just take the first row of our Pandas table.
#Python3 first_row = list(data.loc)
Let’s look into the result:
Python created a list containing the first row values:
Convert a dataframe to a list of lists
We just learnt that we are able to easily convert rows and columns to lists. But what if we want to convert the entire dataframe?
Here we go:
We’ll return the following list of lists:
Convert a Pandas Series to a list
We’ll first going to quickly create a data Series from a Numpy array Then export it back to a list. Here we go:
#Python3 import numpy as np ds = pd.Series(np.array([1,2,3,4,5]) list(ds)
And the output will be a list:
print (list (ds))
[1, 2, 3, 4, 5]
That’ it for today, happy data wrangling 🙂