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:
person | budget | actuals_1 | actuals_3 | actuals_2 | |
---|---|---|---|---|---|
0 | Debbie | 20000 | 25000 | 25000 | 25000 |
1 | Kim | 30000 | 45000 | 45000 | 45000 |
2 | Dorothy | 35000 | 72000 | 72000 | 72000 |
3 | Tim | 17000 | 85000 | 85000 | 85000 |
Hopefully this was useful, feel free to leave me a comment in case of questions or remarks.