How to create a pandas DataFrame from a Python list?

In today’s tutorial we’ll learn how to convert a Python list to a DataFrame or its rows / columns. We will cover several cases:

  • Converting a list to an entire DataFrame.
  • Inserting a list as a DataFrame column
  • Adding column names
  • Appending a list as a row to a DataFrame
  • And for completeness – convert list to a pandas Series object.

Example Data

As we typically do, we’ll start by importing the pandas library and quickly create some test data for the example.

import pandas as pd

# define a  Python list
office_lst = ['New York', 'Tokyo', 'Osaka', 'Washington DC', 'Tel Aviv', 'San Francisco']

#Initialize a pandas DataFrame

month = ['November', 'July', 'September', 'November', 'September', 'October']
language = ['R', 'Python', 'R', 'R', 'Javascript', 'Python']
salary = [139.0, 166.0, 185.0, 98.0, 140.0, 136.0]
hr = dict(month = month, language  =language, salary = salary)
hr_df = pd.DataFrame(data=hr)
hr_df.head(6)

Here’s our data:

monthlanguagesalary
0NovemberR139.0
1JulyPython166.0
2SeptemberR185.0
3NovemberR98.0
4SeptemberJavascript140.0
5OctoberPython136.0

Convert Python list to a DataFrame

We can invoke the pd.DataFrame constructor to create our DataFrame from a list:

office_df = pd.DataFrame  (office_lst, columns = ['city'])

office_df

Will render the following DataFrame

city
0New York
1Tokyo
2Osaka
3Washington DC
4Tel Aviv
5San Francisco

We can also use the transpose (T) method of DataFrames to convert to show our list as a row in the new created DataFrame:

office_df.T

The output will be:

012345
cityNew YorkTokyoOsakaWashington DCTel AvivSan Francisco

Create DataFrame column from list

We can easily merge our list as a DataFrame and create a new column using the insert() DataFrame method:

hr_df.insert(loc=hr_df.shape[1], column = 'city', value = office_lst)

and get this DataFrame. Note the city column that we have added and contains our list values.

monthlanguagesalarycity
0NovemberR139.0New York
1JulyPython166.0Tokyo
2SeptemberR185.0Osaka
3NovemberR98.0Washington DC
4SeptemberJavascript140.0Tel Aviv
5OctoberPython136.0San Francisco

Adding column names

In the snippet shown below, the column parameter allows to specify column names as needed. Note that if appending multiple lists as columns, you’ll need to pass a list containing the column names into the columns parameter.

Append a list as a DataFrame row

Consider the following list:

new_row = ['December', 'Python', 147, 'Chicago']

We can use the loc accessor to append the list by specifying its label. Note that you cannot use the iloc accessor to accomplish this, as iloc can’t be used to enlarge its target object.

hr_df.loc[(len(hr_df))] = new_row

Convert a list to a pandas Series

Converting our list to a Series is easy with the pd.Series constructor:

office_s = pd.Series (office_lst)

Additional Learning