Column rearragement operation

Screen Link:
https://app.dataquest.io/m/238/ordinary-least-squares/1/introduction

Answer Code:

X = X[['bias'] + features]

I know that this code takes the existing columns of dataframe X and moves the ‘bias’ column to become the first column, but I dont recall seeing this method for rearraging columns. What is it called and how does it work? I have seen operations like this:
selected = df[['Age', 'Price_average', 'Email', 'Brand']]
and I know that features is a list of column names, but I dont know what the + is doing there. Thanks!

list + operator concatenate two list together and returns the resultant list.

You can use interactive python to see how + works.

# type command after $ on the terminal
$ python
# Within interactive python shell 
['bias'] + ['Age', 'Price_average', 'Email', 'Brand']
>>> ['bias', 'Age', 'Price_average', 'Email', 'Brand']
1 Like

Thought this was a nice hack at first but led to check out df.insert which is better.

1 Like