How to write Python / Pandas rows and columns into a list?

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.

# Python3
import pandas as pd

data = pd.DataFrame({"Language": ["Ruby", "PHP", "JavaScript", "C-Sharp", "VB.NET", "Python"],
                    "Popularity": [400, 100, 500, 300, 200, 1000]})

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:

[‘Ruby’, ‘PHP’, ‘JavaScript’, ‘C-Sharp’, ‘VB.NET’, ‘Python’]

Note: you can learn how to work with Python DataFrame columns, using our tutorial on creating one or multiple columns in Pandas, splitting a DataFrame column to multiple ones 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.

first_row = list(data.loc[0])

Let’s look into the result:


Python created a list containing the first row values:

[‘Ruby’, 400]

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:

[['Ruby', 400],
 ['PHP', 100],
 ['JavaScript', 500],
 ['C-Sharp', 300],
 ['VB.NET', 200],
 ['Python', 1000]]

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:

import numpy as np
ds = pd.Series(np.array([1,2,3,4,5])

And the output will be a list:

print (list (ds))

[1, 2, 3, 4, 5]

That’ it for today, happy data wrangling 🙂