Can we select columns of a dataframe in any order?


#1

Question

Can we select columns of a Pandas dataframe in any order?

Answer

You certainly can! When selecting multiple columns from a DataFrame, you can order the columns however you would like them to appear. This is particularly useful because if we wanted to see the data in a certain way different from the original column order, we can reorder them in the output however we need.

Example

df = pd.DataFrame([
  ['Doe', 'Jane'],
  ['Law', 'Bob']],
  columns = ['last_name', 'first_name'])

# We can select the first_name column first, 
# then the last_name.
full_names = df[['first_name', 'last_name']]