Skip to content
Advertisement

How to move a column in a pandas dataframe

I want to take a column indexed ‘length’ and make it my second column. It currently exists as the 5th column. I have tried:

JavaScript

I see the following error:

TypeError: must be str, not list

I’m not sure how to interpret this error because it actually should be a list, right?

Also, is there a general method to move any column by label to a specified position? My columns only have one level, i.e. no MultiIndex involved.

Advertisement

Answer

Correcting your error

I’m not sure how to interpret this error because it actually should be a list, right?

No: colnames[0] and colnames[4] are scalars, not lists. You can’t concatenate a scalar with a list. To make them lists, use square brackets:

JavaScript

You can either use df[[colnames]] or df.reindex(columns=colnames): both necessarily trigger a copy operation as this transformation cannot be processed in place.

Generic solution

But converting arrays to lists and then concatenating lists manually is not only expensive, but prone to error. A related answer has many list-based solutions, but a NumPy-based solution is worthwhile since pd.Index objects are stored as NumPy arrays.

The key here is to modify the NumPy array via slicing rather than concatenation. There are only 2 cases to handle: when the desired position exists after the current position, and vice versa.

JavaScript

Performance benchmarking

Using NumPy slicing is more efficient with a large number of columns versus a list-based method:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement