Add Python lists as columns in Pandas DataFrames

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.

#Python3
# 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]})
sales.head()

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 Pandas DataFrame

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 in your DataFrame

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

Example:

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

Here’s our current data:

personbudgetactuals_1actuals_3actuals_2
0Debbie20000250002500025000
1Kim30000450004500045000
2Dorothy35000720007200072000
3Tim17000850008500085000

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

Leave a Comment