A common task you will need to do is to reorder columns in your DataFrame so that they are easier to read or follow some logical order for a report. Reordering columns is similar to as selecting multiple columns, but you change the order. In this article, we will see how to reorder columns in a pandas DataFrame.
To reorder columns you select the columns you would like using the indexing operator, but specify the order you want.
import pandas as pd
df = pd.DataFrame([
{
"person": "James",
"sales": 1000,
"lastName": "Taylor",
},
{
"person": "Clara",
"sales": 3000,
"lastName": "Brown"
}
])
people = df[['sales', 'pearson',]]
print(people)
Notice here that all we did was switch "sales" and "pearson" then pandas created a dataframe with columns in that order. You don't have to specify all columns. As per our example, you see we only select a subset of the data.