Reputation: 35
SUMMARY of my problem: I have many DataFrames, all with the SAME columns, BUT arranged in different orders every time (i.e. df1 has its order, df2 has another order, df3 yet another order and so on...). I want to arrange the columns in each DataFrame based on a list of columns that serves as a benchmark. Therefore all the Dataframes should have the same column order based on this list.
More detailed description: I have several DataFrames in my code that are produced by cleaning some datasets. Each one of these DataFrames has the same columns, BUT in different order one from the other. For example:
DataFrame1
'rank' 'title' 'summary' 'date' 'image' 'id' 'description'
DataFrame2
'date' 'rank' 'title' 'description' 'image' 'summary' 'id'
DataFrame3
'id' 'description' 'summary' 'date' 'rank' 'image' 'title'
and I have a list with column names that i want to use as reference to order the columns in all the DataFrames, for example 'title' 'date' 'rank' 'image' 'description' 'summary' 'id'
is there a way, a loop for example, to arrange the columns this way?
Upvotes: 1
Views: 283
Reputation: 353
col_order = ['title', 'date', 'rank', 'image', 'description', 'summary', 'id']
df1_ordered = df1.reindex(columns = col_order)
df2_ordered = df2.reindex(columns = col_order)
df3_ordered = df3.reindex(columns = col_order)
Upvotes: 0
Reputation: 882
Save your desired order into a list, say list_cols
as follows:
list_cols = ['title', 'date', 'rank', 'image', 'description', 'summary', 'id']
Then change the columns of the dataframes as follows:
df1 = df1[list_cols]
df2 = df2[list_cols]
df3 = df3[list_cols]
Upvotes: 1