Python lists as new columns in Pandas

Our quick data wrangling recipe today covers the topic of adding Python lists to Pandas DataFrames as columns and rows.

Creating the data

We’ll start with a simple dataset that we’ll use throughout this tutorial examples. Go ahead and copy this code into your data analysis Python environment.

# Import Pandas
import pandas as pd

# Now, let's create the dataframe 
sales = pd.DataFrame({
                        "person": ["Debbie", "Kim", "Dorothy", "Tim"],
                     "budget": [20000, 30000, 35000, 17000]})

Let’s also define a simple list for us to later on insert into the DataFrame:

actuals_list = [25000,45000, 72000, 85000]

List as new column in DataFrames

The simplest way to insert the list is to assign it to a new column:

sales['actuals_1'] = actuals_list

Note: Trying to assign a list that doesn’t match the lenght of the Dataframe will result in the following error:

ValueError: Length of values does not match length of index

List to Dataframe Series

An alternative method is to first convert our list into a Pandas Series and then assign the values to a column.

#2. Convert to Series
actuals_s = pd.Series(actuals_list)

# Then assign to the df
sales['actuals_2'] = actuals_s

Inserting the list into specific locations

So far, the new columns were appended to the rightmost part of the dataframe. That said, we can use the insert method to inser the new data into arbitrary location.


# insert to index=3
sales.insert(loc=3, column='actuals_3',value = actuals_s)

Here’s our current data:


Hopefully this was useful, feel free to leave me a comment in case of questions or remarks.

Leave a Comment